PowerShell: Reboot or Shutdown remote machine with Comment

For a long time, I have been looking for a way to Reboot or Shutdown a remote computer with a comment using PowerShell.

The problem arises from the limitation of the Restart-Computer cmdlet. It does not have the option to add a comment. Restart-Computer cmdlet however has the ability to specify an alternate credential. Everywhere this has been discussed, the suggestion was to use the traditional shutdown.exe command. However, shutdown.exe does not give you the option to use alternate credentials.

A workaround to this limitation was to add a cached credential using cmdkey.exe. One could add a generic cached credential for the remote machine and then call the shutdown.exe command on the machine. While this was a perfectly good way to do it, I wasn’t satisfied. So I set out to do my own research.

One day I was messing around with WMI Explorer and curiously clicked on the Win32_OperatingSystem Class. Look what I had found. Being inexperienced as I am, I had no idea how to use it.


So I decided to look up the documentation of the method Win32Shutdowntracker.

This is what I learned. The method takes 4 parameters (as is also seen in the picture above)

  1. Timeout [An Integer Value] in seconds.
  2. Comment [A string value] exactly what I was looking for.
  3. Reason Code [Integer Value] The different values are mentioned here.
  4. Flags [Integer Value] This actually decides what action is performed at the remote machine. The flags are explained in the Win32ShutdownTracker documentation.

So with this information, we can now write a code like this to Force Reboot a remote machine.

(Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer).Win32Shutdowntracker(0, "This is a custom comment", 0x00000000, 6)

Adding a credential object is just as easy now because -Credential is a parameter of the Get-WmiObject Cmdlet.

(Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer -Credential $CustomCredentials).Win32Shutdowntracker(0, "This is a custom comment", 0x00000000, 6)

Once executed, you would receive an output like this:

__GENUS : 2
__PATH :
ReturnValue : 0
PSComputerName :

A Return Value of 0 indicates successful execution

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s