7/10/2008

vbScript to backup and restore the windows taskbar toolbars

1. Create a backup of the toolbar settings
The following vbscript creates a backup of the current users toolbar settings.
It's also possible to target other userprofiles or computers. For this the variables HKEY_CURRENT_USER and strComputer must be customized.
The backupfile will be written at D:\ToolbarBackup.txt
  1. Dim strComputer  
  2. Dim objFile, objFSO  
  3. Dim objRegistry  
  4. Dim strKeyPath  
  5. Dim arrValues, strValue  
  6. Const HKEY_CURRENT_USER = &H80000001  
  7.   
  8. strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\Streams\Desktop"  
  9. strComputer = "."  
  10.   
  11. Set objFSO = CreateObject("Scripting.FileSystemObject")  
  12. Set objFile = objFSO.CreateTextFile("D:\ToolbarBackup.txt")  
  13. Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")  
  14.   
  15. objRegistry.GetBinaryValue HKEY_CURRENT_USER,strKeyPath, "TaskbarWinXP",arrValues  
  16.   
  17. For Each strValue In arrValues  
  18.       objFile.WriteLine strValue  
  19. Next  
  20.   
  21. objFile.Close  
  22. Set objRegistry = Nothing  
  23. Set objFile = Nothing  
  24. Set objFSO = Nothing  


2. Restore backup of the toolbar settings
To restore the toolbar settings, the following vbscript must be executed. It's important that the explorer.exe is terminated when restoring the settings, because else the settings are discarded.
  1. Dim strComputer  
  2. Dim objFile, objFSO  
  3. Dim objRegistry  
  4. Dim strKeyPath, strValues, arrStrValues, arrValues  
  5. Const HKEY_CURRENT_USER = &H80000001  
  6.   
  7. strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\Streams\Desktop"  
  8. strComputer = "."  
  9.   
  10. Set objFSO = CreateObject("Scripting.FileSystemObject")  
  11. Set objFile = objFSO.OpenTextFile("D:\ToolbarBackup.txt")  
  12. Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")  
  13.   
  14. strValues=objFile.ReadAll  
  15. arrStrValues=Split(strValues, vbCrLf)  
  16.    
  17. ReDim arrValues(UBound(arrStrValues)-1)  
  18. for i=0 To UBound(arrStrValues)-1  
  19.  arrValues(i)=CInt(arrStrValues(i))  
  20. Next  
  21.   
  22. For Each Process in GetObject("winmgmts:").ExecQuery ("select * from Win32_Process where name='explorer.exe'")  
  23.  Process.Terminate(0)  
  24. Next  
  25.    
  26. objRegistry.SetBinaryValue HKEY_CURRENT_USER,strKeyPath, "TaskbarWinXP",arrValues      
  27.   
  28. objFile.Close  
  29. Set objRegistry = Nothing  
  30. Set objFile = Nothing  
  31. Set objFSO = Nothing