Search code examples
powershell

How to restart or shutdown a remote computer with comment from powershell


I have been looking for a way to restart/shutdown a remote computer with comment from powershell.

The traditional Restart-Computer Cmdlet does not have these options. then somewhere they advised to use the native command for shutdown.exe. But this command does not support the use of credentials like powershell does.


Solution

  • On a related research, I stumbled upon the WMI Class Win32_OperatingSystem which comes with a method Win32ShutdownTracker.

    The documentation for the Win32Shutdowntracker() method is available here: https://msdn.microsoft.com/en-us/library/aa394057(v=vs.85).aspx

    It takes four parameters as explained below:

    So the code to Force Restart a remote machine becomes:

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

    Adding Credentials through a PScredential object is now easy:

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