I wanted to configure my Vista Home Premium machine to run a script at shutdown, to synchronise "My Documents" to my file server. On my old XP workstation this was done by editing the local group policy and specifying a shutdown script, and then configuring PathSync (a nice free app) to sync the files to a remote location. The challenge this time is there is no local group policy editor on Vista Home premium.
Options
1. Install some software that modifies the shutdown routine
I didn't really want to do this. I've only had this machine a few weeks and want to keep it free of software that could mess things up. I also don't want to pay for a solution, if possible.
2. Windows registry editing
Every local group policy has a corresponding registry entry, so I wondered if in the absence of the group policy editor, I would just be able to create the relevant registry keys and values to run a script at shutdown. Microsoft kindly provide a list of group policies and where they are configured in the registry, so I downloaded it to have a look. Sadly I couldn't find what I needed in here.
I also wondered if I would just be able to export the existing key from my XP machine into Vista, I tried this but that didn't work either - looks like this was because it references the name of a local group policy, which of course didn't exist on my new machine.
3. Windows task scheduler
The Vista task scheduler is actually very good. You can schedule tasks to run when specific events are written to the event log, such as Event 4634 in the security log (Log Off). I tested and found this would actually work if I used the log off feature, but when shutting down, the OS does not wait for scheduled tasks to finish so it only gets to run for a few seconds.
4. Use a VBScript to sync and shutdown
This is how I've implemented this solution - for now. I've created a VB Script which brings up an "Are you sure you want to shutdown" dialog, if you click yes, it syncs to my remote machine and then shuts down. Then I created a shortcut to the script and added it to the quick launch buttons, next to the start menu button. As a final touch I changed the icon to the same power icon that Vista uses for shutdown.
![]()
Shutdown button
The snag is of course you have to remember to click on this button to shutdown if you want your documents to sync, if you forget and use the usual shutdown option on the start menu, you get no backup for that session. This also means that it doesn't sync when you reboot, and you also don't get the option to install Windows updates before the computer shuts down.
Anyway here is the script:
Set wshShell = WScript.CreateObject ("WSCript.shell")
strShutdown = "shutdown -s -t 0 -f"
Continue = MsgBox ("Are you sure you want to shutdown?",vbYesNo + VBCritical, "Shutdown computer")
If Continue = vbNo then
' Do nothing
Else
' This box will disappear after 5 seconds.
intReturn = objShell.Popup("The computer will now sync and shutdown.", 5, "Shutdown computer", VBInformation)
' Run synctoy - just change "MyDocs" to the name of your folder pair.
wshshell.run """c:Program FilesSyncToy 2.0 BetaSyncToyCmd.exe"" -RMyDocs", 6, True
' Shutdown the computer
objShell.Run strShutdown
End If
How do I use this code?
To use the code right-click on your desktop and select New --> Text Document. Copy and paste the code above into the new document. Click on File --> 'Save As...'. Change the filename to shutdown.vbs and the type to 'All files (*.*)'. Once saved you can drop this file on your system anywhere to keep your desktop tidy.

Save As ... All Files
Next you can create a shortcut to the file in your quick launch area or Start Menu and give the shortcut a nice name like 'Sync and Shutdown', and a nicer icon that the generic VBScript icon.
One slight annoyance is the SyncToy command prompt window opens minimised, but I can live with that for the moment. I tried a number of sync tools before settling with MS SyncToy. I did try using rsync which I thought may be a bit faster due to it's compatibility with Linux, but the scanning of the folders took forever.


I ended here looking for the best solution for the same scope. In Vista Ultimate for me the best way to do a backup at shutdown is to use a batch that contains a robocopy script (my syntax: robocopy /MIR /V /R:0 /XJ /TEE /LOG+::\robocopy.log) and to have the GPO execute it after setting gpedit.msc (go in Computer Configuration > Windows Settings > Scripts (Startup/Shutdown) > right click Shutdown > Properties > Add... [your script] > Ok). Not sure if Vista Home Premium gives this possibility.
Hey, cheers for your comment. That would have been my preferred path too (it's how I used to do this on Windows XP Pro) but as you suspected gpedit.msc is not implemented on Vista Home Premium!
@Ben: Yeah, I found out about Home Premium's lack of gpedit.msc the hard way, just a few minutes ago, while trying to write a blog entry about setting up shutdown scripts. Kinda jumped up and bit me in the ass...
Nice script, tho. I'll make sure to point people your direction, and I'll actually probably end up using a variation myself on my new laptop. Keep up the good work!
Mate, Thanks for this, I have been trying to work out a best way to sync for sales guys and you my friend has helped me tremendously to make this process of shutting down and syncing ridiculously easy. I tried Lastchance as a program which worked well, however it just logs you off instead of shutting down in XP which was not really acceptable as these guys would just moan cos they have to do an extra step...Thanks again
@Ben, You have made me spend the last few days coding and refining your idea! I have the same problem, but I store all of my data in a subdirectory off of the root (C:\My Data). This is to ease syncing of data.
Only several programs or settings require me to save data within the Windows standard directory system. These I need to sync into the My Data locations (Start Menu, App Launcher, Favorites, Send To, etc.).
The problem is to sync in a specific order, and to easily add new Folder Pairs. I also wanted to see what SyncToy actually did (could be at a later time) and I was annoyed at looking at blank Cmd windows.
The following is what I came up with, and I hope you might like it as well.
Const ProgramDirectory = "C:\Program Files\SyncToy 2.0" ' The Directory where SyncToy is installed
Const LogsDirectory = "C:\My Data\My System\SyncToy\Logs" ' The Directory where the Log Files are to be written
Const DataFile = "SyncFolder List.txt" ' The name of the SyncFolder data file
Set wshShell = WScript.CreateObject ("WScript.shell") ' Create the WScript Objects to execute the program and display PopUps
Set objFSO = CreateObject("Scripting.FileSystemObject") ' Create the FileSystemObject
Set objDataFile = objFSO.OpenTextFile( wshShell.CurrentDirectory & "\" & DataFile, 1) ' Create the TextStream for the Data File
Set objParameters = WScript.Arguments ' Create Argument object to read the parameters passed to the script
strAction = objParameters(0) ' Store the Action to be taken, Hibernate, Restart, Shutdown, Standby
strCmd = objParameters(1) ' Store the Program and it's parametets
' Ensure that the requested action is desired
lngMsgBox = MsgBox ("Are you sure you want to " & LCase(strAction) & " the system?", _
vbYesNo + VBCritical, strAction & " computer")
If lngMsgBox = vbYes then ' If the user wants to continue, do so
' This box will disappear after 3 seconds. Display action notification
intReturn = wshShell.Popup("The computer will now sync and " & LCase(strAction) & ".", 3, _
strAction & " computer", VBInformation)
Do While Not objDataFile.AtEndOfStream ' Step through the data file and execute the listed Folder Pair
strFolderPair = objDataFile.ReadLine ' Read the line containing the Folder Pair name
If WriteLogs Then ' Set up the CmdLine based on whether or not to create log files
' Use CMD.exe to execute the command line using piping - otherwise it won't work
' && allows Cmd.exe to execute multiple commands on 1 line, so display FolderPair in DOS window
strCmdLine = "Cmd.exe /C Echo Syncing " & strFolderPair & "&&""" & ProgramDirectory & _
"\SyncToyCmd.exe"" -R """ & strFolderPair & """ > """ & LogsDirectory & "\" & strFolderPair & ".log"""""
Else
' Cmd line if no log file is to be written
strCmdLine = "Cmd.exe /C Echo Syncing " & strFolderPair & "&&" & ProgramDirectory & _
"\SyncToyCmd.exe"" -R """ & strFolderPair & """"
End If
wshShell.run strCmdLine, 5, True ' Execute the command
Loop
End If
objDataFile.Close ' Close the datafile
' This box will disappear after 3 seconds. Display Sync Completion and action to be taken
intReturn = wshShell.Popup("Completed syncing files." & vbcrlf & vbcrlf & "The computer will now " & LCase(strAction) & ".", 3, _
strAction & " computer", VBInformation)
Set objResult = wshShell.exec( "%windir%\System32\" & strCmd ) ' Actually carry out desired action
To use the above code, just create a shortcut to it and add the action (Hibernate, Restart, Shutdown, or Standby) as the first parameter. The second parameter is the program name with it's parameters - "Shutdown.exe /h" for Hibernate (/r for Restart, /s for Shutdown). For Standby it is "C:\My Data\My System\SyncToy\Scripts\Sync with Action.vbs" "Standby" "rundll32.exe PowrProf,SetSuspendState"
The code stores path information in constants so there is only 1 place that needs changing.
The && separates commands in CMD statements, so the action being carried out is echoed in the DOS window.
Lastly, the textfile SyncFolderList.txt is simply a text file with a listing of all of the folder pairs I want synced (each on it's own line)
I created a separate file, Sync All.vbs that does all of the syncing without any other action taking place, it is listed below.
Const ProgramDirectory = "C:\Program Files\SyncToy 2.0" ' The Directory where SyncToy is installed
Const LogsDirectory = "C:\My Data\My System\SyncToy\Logs" ' The Directory where the Log Files are to be written
Const DataFile = "SyncFolder List.txt" ' The name of the SyncFolder data file
' Create the WScript Objects to execute the program
Set wshShell = WScript.CreateObject ("WSCript.shell")
Set objFSO = CreateObject("Scripting.FileSystemObject") ' Create the FileSystemObject
Set objDataFile = objFSO.OpenTextFile( wshShell.CurrentDirectory & "\" & DataFile, 1) ' Create the TextStream for the Data File
Do While Not objDataFile.AtEndOfStream ' Step through the data file and execute the listed Folder Pair
strFolderPair = objDataFile.ReadLine ' Read the line containing the Folder Pair name
If WriteLogs Then ' Set up the CmdLine based on whether or not to create log files
' Use CMD.exe to execute the command line using piping - otherwise it won't work
strCmdLine = "Cmd.exe /C Echo Syncing " & strFolderPair & "&&""" & ProgramDirectory & "\SyncToyCmd.exe"" -R """ & _
strFolderPair & """ > """ & LogsDirectory & "\" & strFolderPair & ".log"""""
Else
strCmdLine = "Cmd.exe /C Echo Syncing " & strFolderPair & "&&" & ProgramDirectory & "\SyncToyCmd.exe"" -R """ & strFolderPair & """"
End If
wshShell.run strCmdLine, 5, True ' Execute the command line
Loop
objDataFile.Close ' Close the text file
To solve the problem of making sure to use these to shutdown, etc., I actually removed all of the shutdown options from the Start Menu and have these shortcuts at the top of my App Launcher on the SideBar.
I hope you and others will find this helpful.
Just a correction note. The End If statement in the first code listing should be moved to the end of the file. Sorry guys, forgot to move it from a debugging position.
Awesome - thanks!