Stop-Computer shuts a machine down, and Restart-Computer restarts it. Both work on the computer in front of you and on remote ones, and both take a list of machines.
Underneath, they call the Win32Shutdown method of the Win32_OperatingSystem WMI class, which is what this article originally described. That method still works and is documented further down, but you no longer have to call it yourself.
Shut down or restart the local computer
Stop-Computer
Restart-Computer
To restart without waiting for applications to close:
Restart-Computer -Force
Stop-Computer calls a WMI method that requires SeShutdownPrivilege. Administrator accounts have it.
Shut down or restart a remote computer
Pass the machine name to -ComputerName, which accepts several at once:
Stop-Computer -ComputerName Server01, Server02
Restart-Computer -ComputerName Server01 -Force
To connect as a different account:
Stop-Computer -ComputerName Server01 -Credential (Get-Credential)
Use -WsmanAuthentication to choose the authentication method when the connection runs over WS-Management:
Stop-Computer -ComputerName Server01 -WsmanAuthentication Kerberos
Rebooting a Remote Desktop session host is a different problem, because everyone signed in loses whatever they have open. How to reboot a terminal server without losing user data covers warning the users and draining the sessions first.
Log off a session
Neither cmdlet logs a user off, so the built-in logoff command still does that job:
logoff
On a Remote Desktop server a session is often disconnected rather than closed, and the three ways to end one are not interchangeable. Disconnect, log off, or reset an RDS session explains which one you want and what each costs.
The Win32Shutdown method behind the cmdlets
Win32Shutdown is a method of the Win32_OperatingSystem class. It takes a flag that says what to do:
| Flag | Action |
|---|---|
| 0 | Log off |
| 1 | Shut down |
| 2 | Reboot |
| 8 | Power off |
| 4 | Forced log off |
| 5 | Forced shut down |
| 6 | Forced reboot |
| 12 | Forced power off |
Adding the Force flag (4) to a command value forces that action. The method returns zero on success, and any other number is an error. Forcing a shutdown or reboot on a remote computer tears down WMI and COM with everything else, so the return value in that case is not meaningful.
These examples call the method through Get-WmiObject, aliased as gwmi:
# Reboot the local computer
(gwmi win32_operatingsystem).Win32Shutdown(2)
# Force a reboot on a remote computer
(gwmi win32_operatingsystem -ComputerName MyServer).Win32Shutdown(6)
# Power off a remote computer with different credentials
(gwmi win32_operatingsystem -ComputerName MyServer -cred (Get-Credential)).Win32Shutdown(12)
They work in Windows PowerShell 5.1 and earlier. PowerShell 7 removed Get-WmiObject along with the rest of the WMI v1 cmdlets, and the CIM cmdlets Get-CimInstance and Invoke-CimMethod took their place. For shutting a computer down there is no reason to call either one, because Stop-Computer and Restart-Computer reach the same method.