Skip to content
Menu

Restart a PC - Now, at a Set Time or If Pending

Schedules a Windows restart with a countdown and a message: now, at a set time, or only when a restart is pending. Needs DryRun=false and Approve=restart-computer.

Restart a PC - now, at a set time or if pending is free for members.

Enter your email to create your free account and get the file.

We'll email you the script and the weekly MSP Outpost email. Unsubscribe anytime. Privacy policy.

Already a member? Sign in.

Be the first to review Restart a PC - now, at a set time or if pending. Tell other MSPs how it worked for you.

Write a Review

LanguagePowerShell
Runs onWindows 10, Windows 11, Windows Server 2016+; runs as SYSTEM
NeedsPowerShell 5.1, administrator rights
Changes thingsChanges what it runs on

What It Does

This script changes nothing until you set DryRun=false and Approve=restart-computer. Before that it lists what it would schedule. It refuses to make more than 25 changes in one run unless you raise MaxChanges.

It schedules a Windows restart with shutdown.exe /r /t and a message. Windows shows the message and a countdown to anyone signed in, so nobody is restarted without warning. It changes nothing else: no settings, no files and no services. IfPending, the default, restarts only when Windows says a restart is pending: component servicing, Windows Update, the Configuration Manager client when installed, or a pending computer rename. Queued file renames alone do not count. Now restarts after DelaySeconds, and At restarts at the next HH:mm in local time, today if it is more than 30 seconds away, otherwise tomorrow.

SkipIfUserActive, on by default, schedules nothing and ends SKIPPED when somebody is signed in and the session is active, not locked or disconnected. When=At ignores it, because the restart happens later. Windows closes apps without asking when the countdown ends, so unsaved work can be lost.

A server or domain controller is not restarted unless AllowServers is true. CancelPending cancels a scheduled restart with shutdown.exe /a and does nothing else. If a restart is already scheduled, running it again says OK and leaves it alone.

Before You Run It

  • Run it as a dry run first. Read when it would restart the PC.
  • Keep SkipIfUserActive on for Now and IfPending to avoid restarting a PC someone is using. For At, choose a time when the user can be interrupted.
  • Write a clear Message. It is what the user sees in the countdown, with no double quotes.
  • Use CancelPending with Approve if a restart was scheduled in error.

Type and Risk

  • Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
  • Risk: High. It is a dry run unless you set both DryRun=false and Approve=restart-computer, and it stops before making more than 25 changes in one run unless you raise MaxChanges. Run it once as a dry run and read the list first.
  • What it changes: Schedules a restart with a warning countdown (shutdown.exe), or cancels a scheduled one.
  • Restart: It restarts the machine itself: that is its job, or an input you set turns it on. The answer says when.
  • Typical run: Under 10 seconds.
  • Example answer: "Restart scheduled for 2026-11-17 02:30 -07:00 (in 5 hours); signed-in users see the Windows warning"
  • Tested before listing on Windows 11 with Windows PowerShell 5.1, run as SYSTEM, against its own test cases.

Inputs

Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.

  • When: IfPending (default), Now or At.
  • At: Time of day for When=At, HH:mm, 24-hour, local (for example 02:30).
  • DelaySeconds: Countdown before a restart for When=Now or IfPending. Whole number, 0 to 86400. Default 300.
  • Message: Text Windows shows to signed-in users, up to 512 characters, no double quotes.
  • SkipIfUserActive: true (default) to skip when a user is signed in and active. When=At ignores it.
  • AllowServers: true to allow a restart on a server or domain controller. Default false.
  • CancelPending: true to cancel a scheduled restart and do nothing else. Default false.

Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.

License

Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.

The Script

restart-computer.ps1 · 1010 lines · 50.8 KB

Free for Members

Create a free account or sign in to read, copy and download this script.

Welcome Back

Sign in to your MSP Outpost account.