Mastering the default Explorer views for Windows XP and 2003

by Jeremy on May 9, 2009

I finally mastered the Windows Explorer views under Windows XP and 2003!

The following script is fully documented.

Enjoy!

SetExplorerViews.vbs


'================================================================================
'
' This script sets the Details view for all folders.
'
' Release 1.3 on 22nd April 2009.
' Created by Jeremy@jhouseconsulting.com on 6th February 2007.
'
' On a Per-User basis, folder views are managed under two registry keys:
' 1) Network Folders - HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell
' 2) Local Folders   - HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam
' Just so you don't get confused let me explain that drive mappings are considered
' to be local folders. Network folders are UNC paths.
'
' How to configure the default Folder Type template for users
'   Create the AllFolders key under the following key:
'   - HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam\Bags\AllFolders
'   - Then create a Shell key under that.
'   - Set the following values...
'   - DWORD value WFlags set to 0
'   - DWORD value Mode set to 4
'   - String value vid set to "{137E7700-3573-11CF-AE69-08002B2E1262}"
'
' Note the following table can be used to configure the above values (WFlags
' should always be 0)
' View Mode vid
' Icons (medium) 1 {0057D0E0-3573-11CF-AE69-08002B2E1262}
' Icons (small) 2 {0057D0E0-3573-11CF-AE69-08002B2E1262}
' List 3 {0E1FA5E0-3573-11CF-AE69-08002B2E1262}
' Details 4 {137E7700-3573-11CF-AE69-08002B2E1262}
' Thumbnail 5 {8BEBB290-52D0-11D0-B7F4-00C04FD706EC}
' Tiles 6 {65F125E5-7BE1-4810-BA9D-D271C8432CE3}
' Filmstrip 7 {8EEFA624-D1E9-445B-94B7-74FBCE2EA11A}
'
' Note that if you don't want to show the status bar in the default folder view
' create a Status DWORD value and set it to 0
'
' Rather than creating this individually for each user you may choose to create
' it globally by setting the exact same values in the HKEY_LOCAL_MACHINE registry
' hive instead. The ShellNoRoam key does not exist in the HKLM hive by default.
'
' This script will also set/reset the view of any existing cached folders. To do
' this it uses WMI to enumerate all the registry subkeys under the following keys:
' - "HKCU\Software\Microsoft\Windows\ShellNoRoam\Bags"
' - "HKCU\Software\Microsoft\Windows\Shell\Bags"
'
' It then uses WMI to create/modify the Mode and Vid values under the following
' keys, where Nodeslot_number is the enumerated subkey:
' - "HKCU\Software\Microsoft\Windows\ShellNoRoam\Bags\Nodeslot_number\Shell"
' - "HKCU\Software\Microsoft\Windows\Shell\Bags\Nodeslot_number\Shell"
'
' You can choose to leave existing folder views as they are by setting the
' blnChangeExistingFolders value to False. This allows users to change the view
' as they wish, without it being overwritten next time they log in.
'
' Note that you must close and reopen Windows Explorer to see the changes.
'
' Usage:
' cscript.exe //nologo SetExplorerViews.vbs
'
' This script was based on...
' 1) A kixtart script written by Rick Mack.
' 2) A blog by Nicholas Dille from Sepago found here:
'    http://blogs.sepago.de/nicholas/2009/02/17/preserving-windows-explorer-folder-views-in-roaming-profiles/
' 3) An article by "Snakefoot" found here:
'    http://smallvoid.com/article/winnt-default-folder-view.html
'
'================================================================================

Option Explicit

Dim objShell, objReg, strComputer, strKeyRoot, strKeyPath, strValueName, strValue
Dim Return, blnDebug, arrSubKeys, subkey, i, strSystemRoot, strCommandLine, strView
Dim arrKeys, item, intMode, strVid, blnChangeExistingFolders

Const HKEY_CURRENT_USER = &H80000001

blnDebug = False

arrKeys = Array("ShellNoRoam","Shell")

blnChangeExistingFolders = True

' Set strView to either medium icons, small icons, list, details, thumbnail, tiles
' or filmstrip
strView = "Details"

' Folder View Mode Values under Bags\Nodeslot_number\Shell. All values are DWORD
Select Case lcase(strView)
  Case "medium icons" 'The view should display medium-size icons
    intMode = 1
    strVid = "{0057D0E0-3573-11CF-AE69-08002B2E1262}"
  Case "small icons" 'The view should display small icons
    intMode = 2
    strVid = "{0057D0E0-3573-11CF-AE69-08002B2E1262}"
  Case "list" 'Object names are displayed in a list view
    intMode = 3
    strVid = "{0E1FA5E0-3573-11CF-AE69-08002B2E1262}"
  Case "details" 'Object names and other selected information, such as the size or date last updated, are shown
    intMode = 4
    strVid = "{137E7700-3573-11CF-AE69-08002B2E1262}"
  Case "thumbnail" 'The view should display thumbnail icons
    intMode = 5
    strVid = "{8BEBB290-52D0-11D0-B7F4-00C04FD706EC}"
  Case "tiles" 'The view should display large icons
    intMode = 6
    strVid = "{65F125E5-7BE1-4810-BA9D-D271C8432CE3}"
  Case "filmstrip" 'The view should display icons in a filmstrip format
    intMode = 7
    strVid = "{8EEFA624-D1E9-445B-94B7-74FBCE2EA11A}"
End Select

' FFlags Values under Bags\Nodeslot_number\Shell. All values are DWORD
' - Autoarrange_Off_Align_to_Grid_Off = 220
' - Autoarrange_On = 221
' - Autoarrange_off_Align_to_Grid_On = 224
' - Autoarrange_On_Align_to_Grid_On = 225

' Sort Values under Bags\Nodeslot_number\Shell. All values are DWORD
' - Sort_by_Name = 0
' - Sort_by_Size = 1
' - Sort_by_Type = 2
' - Sort_By_Modified = 3

strComputer = "."

Set objShell = WScript.CreateObject("WScript.Shell")
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")

strKeyRoot = "HKCU\"
strKeyPath = "Software\Microsoft\Windows\"

' New profiles will not contain the key structure, so we need to create them.
For Each item in arrKeys
  If NOT RegKeyExists(strKeyRoot & strKeyPath & item) Then
    Return = objReg.CreateKey (HKEY_CURRENT_USER,strKeyPath & item)
    If blnDebug Then
      If (Return = 0) And (Err.Number = 0) Then
        Wscript.Echo "Create the " & strKeyPath & item & " key."
      Else
        Wscript.Echo "Could not create the " & strKeyPath & item & " key."
      End If
    End If
  End If
  If NOT RegKeyExists(strKeyRoot & strKeyPath & item & "\Bags") Then
    Return = objReg.CreateKey (HKEY_CURRENT_USER,strKeyPath & item & "\Bags")
    If blnDebug Then
      If (Return = 0) And (Err.Number = 0) Then
        Wscript.Echo "Create the " & strKeyPath & item & "\Bags key."
      Else
        Wscript.Echo "Could not create the " & strKeyPath & item & "\Bags key."
      End If
    End If
  End If
  If NOT RegKeyExists(strKeyRoot & strKeyPath & item & "\BagMRU") Then
    Return = objReg.CreateKey (HKEY_CURRENT_USER,strKeyPath & item & "\BagMRU")
    If blnDebug Then
      If (Return = 0) And (Err.Number = 0) Then
        Wscript.Echo "Create the " & strKeyPath & item & "\BagMRU key."
      Else
        Wscript.Echo "Could not create the " & strKeyPath & item & "\BagMRU key."
      End If
    End If
  End If
Next

' The ShellNoRoam fix as per the Sepago article.
strValueName = ""
strValue = "%COMPUTERNAME%"
Return = objReg.SetExpandedStringValue (HKEY_CURRENT_USER,strKeyPath & "ShellNoRoam",strValueName,strValue)
If blnDebug Then
  If (Return = 0) And (Err.Number = 0) Then
    Wscript.Echo "SetExpandedStringValue succeeded"
  Else
    Wscript.Echo "SetExpandedStringValue failed. Error = " & Err.Number
  End If
End If

' Create the AllFolders key, and values within.
Return = objReg.CreateKey (HKEY_CURRENT_USER,strKeyPath & "ShellNoRoam\Bags\AllFolders")
If blnDebug Then
  If (Return = 0) And (Err.Number = 0) Then
    Wscript.Echo "Create the " & strKeyPath & "ShellNoRoam" & "\Bags\AllFolders key."
  Else
    Wscript.Echo "Could not create the " & strKeyPath & "ShellNoRoam\Bags\AllFolders key."
  End If
End If
Return = objReg.CreateKey (HKEY_CURRENT_USER,strKeyPath & "ShellNoRoam\Bags\AllFolders\Shell")
If blnDebug Then
  If (Return = 0) And (Err.Number = 0) Then
    Wscript.Echo "Create the " & strKeyPath & "ShellNoRoam\Bags\AllFolders\Shell key."
  Else
    Wscript.Echo "Could not create the " & strKeyPath & "ShellNoRoam\Bags\AllFolders\Shell key."
  End If
End If
Return = objReg.SetDWORDValue (HKEY_CURRENT_USER,strKeyPath & "ShellNoRoam\Bags\AllFolders\Shell","WFlags",0)
If blnDebug Then
  If (Return = 0) And (Err.Number = 0) Then
    Wscript.Echo "SetDWORDValue succeeded"
  Else
    Wscript.Echo "SetDWORDValue failed. Error = " & Err.Number
  End If
End If
Return = objReg.SetDWORDValue (HKEY_CURRENT_USER,strKeyPath & "ShellNoRoam\Bags\AllFolders\Shell","Mode",intMode)
If blnDebug Then
  If (Return = 0) And (Err.Number = 0) Then
    Wscript.Echo "SetDWORDValue succeeded"
  Else
    Wscript.Echo "SetDWORDValue failed. Error = " & Err.Number
  End If
End If
Return = objReg.SetStringValue (HKEY_CURRENT_USER,strKeyPath & "ShellNoRoam\Bags\AllFolders\Shell","Vid",strVid)
If blnDebug Then
  If (Return = 0) And (Err.Number = 0) Then
    Wscript.Echo "SetStringValue succeeded"
  Else
    Wscript.Echo "SetStringValue failed. Error = " & Err.Number
  End If
End If

' Enumerate and change existing folder values.
If blnChangeExistingFolders Then
  For Each item in arrKeys
    Return = objReg.EnumKey (HKEY_CURRENT_USER, strKeyPath & item & "\Bags", arrSubKeys)
    If blnDebug Then
      If (Return = 0) And (Err.Number = 0) Then
        Wscript.Echo "EnumKey succeeded"
      Else
        Wscript.Echo "EnumKey failed. Error = " & Err.Number
      End If
    End If
    If blnDebug Then
'     The next three lines for debugging purposes only to display the contents of
'     the array elements.
      For i = 0 to UBound(arrSubKeys)
        Wscript.echo "Array element " & i & " contains a value of " & arrSubKeys(i)
      Next
    End If
    If IsArray(arrSubKeys) Then
      For Each subkey In arrSubKeys
        Return = objReg.SetDWORDValue (HKEY_CURRENT_USER,strKeyPath & item & "\Bags\" & subkey & "\" & "Shell","Mode",intMode)
        If blnDebug Then
          If (Return = 0) And (Err.Number = 0) Then  
            Wscript.Echo "SetDWORDValue succeeded"
          Else
            Wscript.Echo "SetDWORDValue failed. Error = " & Err.Number
          End If
        End If
        Return = objReg.SetStringValue (HKEY_CURRENT_USER,strKeyPath & item & "\Bags\" & subkey & "\" & "Shell","Vid",strVid)
        If blnDebug Then
          If (Return = 0) And (Err.Number = 0) Then  
            Wscript.Echo "SetStringValue succeeded"
          Else
            Wscript.Echo "SetStringValue failed. Error = " & Err.Number
          End If
        End If
      Next
    End If
  Next
End If

Set objShell = Nothing
Set objReg = Nothing

WScript.Quit(0)

Function RegKeyExists(ByVal sRegKey)
' Returns True or False based on the existence of a registry key.
  Dim sDescription, oShell
  Set oShell = CreateObject("WScript.Shell")
  RegKeyExists = True
  sRegKey = Trim (sRegKey)
  If Not Right(sRegKey, 1) = "\" Then
    sRegKey = sRegKey & "\"
  End If
  On Error Resume Next
  oShell.RegRead "HKEYNotAKey\"
  sDescription = Replace(Err.Description, "HKEYNotAKey\", "")
  Err.Clear
  oShell.RegRead sRegKey
  RegKeyExists = sDescription <> Replace(Err.Description, sRegKey, "")
  On Error Goto 0
  Set oShell = Nothing
End Function

Related posts:

  1. Script to remove orphaned/stale printer objects
  2. Using WMI to detect if running on a Windows 2003 or 2008 Terminal Server
  3. Microsoft Windows 2003 Post-SP2 Hotfixes

Leave a Comment

 

Previous post:

Next post: