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

Dim strComputer
Dim objFile, objFSO
Dim objRegistry
Dim strKeyPath
Dim arrValues, strValue
Const HKEY_CURRENT_USER = &H80000001

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\Streams\Desktop"
strComputer = "."

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("D:\ToolbarBackup.txt")
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

objRegistry.GetBinaryValue HKEY_CURRENT_USER,strKeyPath, "TaskbarWinXP",arrValues

For Each strValue In arrValues
objFile.WriteLine strValue
Next

objFile.Close
Set objRegistry = Nothing
Set objFile = Nothing
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.

Dim strComputer
Dim objFile, objFSO
Dim objRegistry
Dim strKeyPath, strValues, arrStrValues, arrValues
Const HKEY_CURRENT_USER = &H80000001

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\Streams\Desktop"
strComputer = "."

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("D:\ToolbarBackup.txt")
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

strValues=objFile.ReadAll
arrStrValues=Split(strValues, vbCrLf)

ReDim arrValues(UBound(arrStrValues)-1)
for i=0 To UBound(arrStrValues)-1
arrValues(i)=CInt(arrStrValues(i))
Next

For Each Process in GetObject("winmgmts:").ExecQuery ("select * from Win32_Process where name='explorer.exe'")
Process.Terminate(0)
Next

objRegistry.SetBinaryValue HKEY_CURRENT_USER,strKeyPath, "TaskbarWinXP",arrValues

objFile.Close
Set objRegistry = Nothing
Set objFile = Nothing
Set objFSO = Nothing

2 comments:

PB said...

Great script...
This was what i was searching for...
Task bar customization was last task of one of our projects and we were bit stuck on it...

Thanks for the help.

Vishwajeet said...

Thanks a lot.The script help us and solved our problem.And with the help of this script we completed our project as it was the last item to complete.
Thank you very much.