6

I have created a script I want to deploy on my XP workstations as a shutdown script. I know I can add my script as a shutdown script with the UI (gpedit.msc), but I want to automate the deployment of my script. My workstations are not part of a Windows domain. I will deploy with OCS Inventory.

I tried to add entries to the Windows registry, but this doesn't work. I don't see what I added when I run gpedit.msc. If I add something with gpedit.msc, this seem to overwrite what I added manually into the registry.

9

To anyone struggling to get this working, my sympathy. I spent many hours trying to figure out exactly which of the hundreds of changes gpedit makes are actually important. My tests were conducted on Windows Server 2016. These turned out to be relevant:

  1. The values under:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\[Startup|Shutdown]\0
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\[Startup|Shutdown]\0\0
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\[Startup|Shutdown]\0
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\[Startup|Shutdown]\0\0
    
  2. The directories:

    \Windows\system32\GroupPolicy\Machine\Scripts\Startup
    \Windows\system32\GroupPolicy\Machine\Scripts\Shutdown
    

Windows uses those directories as the working directory for the scripts. Even if you don't keep your startup/shutdown scripts there, those directories need to exist.

Nothing else gpedit does seemed to matter. This includes many registry entries and the .ini files referenced in other answers. It's possible that some of the keys above are superfluous as well but I ran out of testing patience.

3

We'll pretend our startup script is located at C:\MyStartupScript.bat

You need a REG_SZ registry entry called Script with its value set to C:\MyStartupScript.bat located at both of the following two locations:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup\0\0

HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\System\Scripts\Startup\0\0

You also need the following entry in C:\WINDOWS\system32\GroupPolicy\Machine\Scripts\Scripts.ini

[Startup]
0CmdLine=C:\MyStartupScript.bat
0Parameters=

For a shutdown script, follow the same instructions but replace every instance of the word Startup with the word Shutdown

edit:

Looks like you may need to create two other registry entries as well if they don't already exist.

You'll need to create them in both of the registry locations mentioned above.

The first entry is a REG_SZ entry called Parameters and its value can be left empty.

The second entry is a REG_QWORD entry called ExecTime and its value is set to 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

In Windows XP, the REG command can't add a REG_QWORD entry to the registry.

To get around this limitation, I, for example, use a batch file that adds these lines

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup\0\0]
"ExecTime"=hex(b):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00

to a file called addQword.reg then executes the file using using this command:

regedit /s addQword.reg
  • Thanks for including the scripts.ini file. Mine works with just the first registry key and this file (although my script is powershell so it's called psscripts.ini) – Andrew Brennan Oct 22 '18 at 13:54
3

I got it working.

I added the script with gpedit.msc on one computer. I exported registry keys from HKLM\Software\Policies\Microsoft\Windows\System\Scripts and HKLM\Software\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine. I also zipped the C:\WINDOWS\SYSTEM32\GroupPolicy directory.

On a different computer, I patched the registry with my 2 .reg files I exported earler on my first computer. I also unzipped my zip file to the same location on the second PC.

The script is running on shutdown and I see it in gpedit.msc. Everything seem to be good!

  • Is there a way to do this without manually unzipping files? I.E. unzipping from a script? – jjxtra Feb 19 '18 at 18:50
1

Small update to Francis answer.

For Windows Server 2016 HKLM\Software\Policies\Microsoft\Windows\System\Scripts was moved to HKLM\Software\Microsoft\Windows\CurrentVersion\Group Policy\Scripts

So I got it working by exporting following registry keys:

HKLM\Software\Microsoft\Windows\CurrentVersion\Group Policy\Scripts HKLM\Software\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine

and as Francis suggested copying

C:\WINDOWS\SYSTEM32\GroupPolicy

0

You can add keys here: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Shutdown

Look here for more information: http://en.kioskea.net/faq/3358-execute-a-script-a-startup-and-shutdown

You will just have to make a reg file that will run on all your machines to add the key to the registry.

  • This was not working. The entries seem to be duplicated in the windows registry. I also found similar keys in HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\System\Scripts\Shutdown. Should I add both entries in the registry? – Francis Apr 10 '12 at 19:50
  • Mmm doesn't work better. The entry is not shown on the gpedit.msc UI and if I add something there this overwrite what I added in the registry. Seem like there is some caching somewhere... – Francis Apr 10 '12 at 20:08
  • you also need the scripts.ini file from Eric Glass's answer – Andrew Brennan Oct 22 '18 at 14:01
0

I created a shutdown script using gpedit.msc on one server, then exported the registry key and imported it into the target servers. Use the following key:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts]

You'll also need to copy the below file to the target:

C:\Windows\system32\GroupPolicy\Machine\Scripts\scripts.ini

If it's a Powershell script then copy the following file instead:

C:\Windows\system32\GroupPolicy\Machine\Scripts\psscripts.ini

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.