Shutdown / Restart computer with comment

When I shutdown a server using the GUI (Start -> Shutdown) , there is a comment box that allows you to enter comments.

I don’t see a parameter to specify a comment with the Shutdown-Computer or Restart-Computer cmdlets.

Does anyone know of an alternative way to specify comments when shutting down / restarting a server via PowerShell

The best I could figure out was following this discussion: How to invoke the shutdown event tracker when running restart-computer

In a nutshell use the native Shutdown command with a /c switch…

Thanks Greg. This helps.Looks like I’ll need to use shutdown.exe for now.

It would be nice if the PowerShell cmdlets would provide full fidelity with their old command-line counterparts. Oh well - maybe in a future version.

Thanks,
Mario

Sounds like a good feature request to add on connect: http://connect.microsoft.com/PowerShell

Stop- and Restart-Computer aren’t technically counterparts to Shutdown.exe. They accomplish a similar task, but through completely different APIs - and it’s the underlying APIs, in this case, that don’t readily support sending the notification. Jason’s correct in that a Connect ticket would be the best way to ask for the functionality. Keep in mind that the PowerShell team doesn’t “own” the entire API stack, so sometimes they’re limited in what they can provide us.

Which is also why PowerShell enables you to user external command-line utilities. No reason not to use Shutdown.exe, really, if it’s what does the job.

It seems I am 3 years late to the party but this has been something I have been looking for a while. The traditional Shutdown.exe method however, does not really support the use of credentials. I could perhaps use cmdkey to cache a credential and trick it to use credential in that manner. So in my quest for a better way, I stumbled upon the Win32_OperatingSystem WMI Class which comes with a Method called Win32ShutdownTracker. Here is the documentation for it: https://msdn.microsoft.com/en-us/library/aa394057(v=vs.85).aspx

If we take a look, running the below code will show us the format to be used.

(Get-WmiObject Win32_OperatingSystem).Win32Shutdowntracker

System.Management.ManagementBaseObject Win32ShutdownTracker(System.UInt32 Timeout, System.String Comment, System.UInt32 ReasonCode, System.Int32 Flags)

Going by the information so far, we can provide an Integer Timeout, a string comment , an integer Reason Code and the Flag which will actually perform the action.

A list of reason codes can be found here: https://msdn.microsoft.com/en-us/library/windows/desktop/aa376885(v=vs.85).aspx
The flags are mentioned in the Win32Shutdowntracker documentation mentioned earlier. Now we have everything we need. So, the final code to restart a machine would look like

    (Get-WmiObject Win32_OperatingSystem -ComputerName $Computer).Win32Shutdowntracker(0, "This is my Custom Comment", 0x00000000 , 6)

Adding a credential now just becomes easier.

(Get-WmiObject Win32_OperatingSystem -ComputerName $Computer -Credential $PSCredential).Win32Shutdowntracker(2, "This is my custom Comment", 0x00000000 , 6)

Where $PScredential is a PScredential Object.

Running this will give a result like this:
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
PSComputerName :

The Return Value of 0 indicates successful execution.