With the virt-win-reg tool built on top of libguestfs and hivex it’s now relatively straightforward to modify a Windows virtual machine so that it runs a batch file, script or program at next boot.
Note: The Windows VM must be shut down before you attempt this.
The plan is that we upload the batch script to some place in the VM, and then add a “RunOnce” key in the Windows Registry (explained in this MSDN article and this article). First let’s just take a look at what’s in the key. In most cases it will be empty:
# virt-win-reg Windows7x64 \
'HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce'
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce]
Now we’ll prepare our batch file and upload it:
# cat test.bat
ECHO HELLO > C:\TEST.LOG
TIME /T >> C:\TEST.LOG
# guestfish -i Windows7x64
Welcome to guestfish, the libguestfs filesystem interactive shell for
editing virtual machine filesystems.
Type: 'help' for a list of commands
'man' to read the manual
'quit' to quit the shell
><fs> upload test.bat /test.bat
><fs> ^D
And finally we modify the RunOnce registry key:
# virt-win-reg --merge Windows7x64 [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce] "Test"="c:\\test.bat" ^D
One potential gotcha: You must be running hivex ≥ 1.2.2.
Now you can boot your Windows guest and check that the script runs after the user logs in. Look for the file C:\TEST.LOG:
><fs> cat /TEST.LOG HELLO 09:21
Because we’re using the RunOnce key, the script will run just one time. If you want it to run every time, use the Run key.
Now, how do we make the script work without the user needing to log in? (Clue: The answer is not RunServicesOnce — this does not work in Windows 7). What’s surprising (coming from a Linux background) is the huge amount of incomplete, contradictory and simply false information contained in MSDN about this topic.

Not sure if it would run if a non-admin would log in to the VM.
That’s right … see my next posting!
Pingback: Tip: Install a service in a Windows VM « Richard WM Jones
Where have you seen false or contradictory information in the MSDN?
One should read the MSDN very carefully, every word there is meaning full. sometimes the most important info (such as what applies to what OS) is listed in a table at the end.
Reading the remarks is MOST important (but as I said you really need to read everything..)
But IMHO, the amount of incorrect information there is very little. and usually someone would point that out in the comments.
Pingback: Tip: Install a device driver in a Windows VM « Richard WM Jones
I agree that MSDN can be confusing, but I think I hardly ever found something _incorrect_ there.
The main problem with RunOnce is that some services don’t allow you to access the console and only give access to Remote Desktop. If the RunOnce program is necessary to give you network access, for example, this won’t work. Installing a service using RHSrvAny could be a good idea for this.
Try finding out which Windows versions support RunServicesOnce, using only MSDN.