How can I create a windows shutdown script from powershell/command-line?

7,203

Group Policy assumes you are in an Active Directory domain environment, in which case Group Policy Objects (GPOs) are centrally managed and assigned to computers. For individual machines, the Local Security Policy can be used to produce similar results to centrally managed GPOs.

Typically in Active Directory, login scripts are assigned to User objects, though you can also do this through Group Policy.

If you're not using Active Directory, or you only want to configure this on a relatively small number of computers, you can certainly do this with PowerShell using the Task Scheduler. With Task Scheduler you can create events to run at startup, session end, or logon.

Share:
7,203

Related videos on Youtube

David Rubin
Author by

David Rubin

Updated on September 18, 2022

Comments

  • David Rubin
    David Rubin almost 2 years

    I've read the TechNet pages that describe using computer/user startup/shutdown scripts, and that's great, but I'd like to create those scripts via the command-line (and not have to click around in gpedit.msc).

    It looks like scripts.ini and psscripts.ini in %SYSTEMROOT%\System32\GroupPolicy\Machine\Scripts specifies the scripts to run, but those don't exist until running gpedit.msc for the first time. Is it safe to create and edit those directly? Or do I need to muck around with Set-GPO or something similar? Thanks!

    • Admin
      Admin about 10 years
      Create your shutdown script, deploy to computers, tell users to use the new shortcut on their desktop (that points to your script) to shutdown their computers. You may have to deal with local permissions for users if they are restricted. Otherwise alternative is to just use GPO.
    • David Rubin
      David Rubin about 10 years
      Thank you for your suggestion, but as I commented below, the shutdown is externally triggered, and there are no users with sessions running to click shortcuts.
    • mklement
      mklement over 5 years
      stackoverflow.com/a/52585110/45375 uses PowerShell to script installation of shutdown scripts, but note its caveats.
  • David Rubin
    David Rubin about 10 years
    Thank you for your response. When creating a task in Task Scheduler, I see "At Log On" and "At Startup", but not "At Shutdown". Are you certain? Googling points towards using "Local Computer Policy", which is why I was pursuing that approach in the first place.
  • Tim Ferrill
    Tim Ferrill about 10 years
    You're correct. You can kick off tasks when a session ends, such as when a user logs off, but not at the shutdown event itself. If a user is initiating the shutdown this would have the same effect. If the shutdown is automated perhaps there is another event that would coincide with the shutdown?
  • David Rubin
    David Rubin about 10 years
    Unfortunately I cannot - the shutdown is externally triggered (i.e. VM gets forcefully terminated, etc.).