This article represents shutting down or rebooting a local or remote computer from PowerShell. You can shutting down or rebooting a computer using Win32Shutdown method of the Win32_OperatingSystem WMI class.
The Win32_OperatingSystem WMI class represents an operating system installed on a Windows computer system. The Win32_OperatingSystem class has a Win32Shutdown method. The Win32Shutdown method provides the full set of shutdown options supported by Win32 operating systems.
uint32 Win32Shutdown( sint32 Flags, sint32 Reserved );
Flags - Bitmapped set of flags to shut the computer down. To force a command, add the Force flag (4) to the command value. Using Force in conjunction with Shutdown or Reboot on a remote computer immediately shuts down everything (including WMI, COM, and so on), or reboots the remote computer. This results in an indeterminate return value.
0 – Log Off 4 – Forced Log Off 1 – Shutdown 5 – Forced Shutdown 2 – Reboot 6 – Forced Reboot 8 – Power Off 12 – Forced Power Off
Reserved - A means to extend Win32Shutdown. Currently, the Reserved parameter is ignored.
Returns zero (0) to indicate success. Any other number indicates an error.
(Get-WmiObject -Class Win32_OperatingSystem -ComputerName .).InvokeMethod("Win32Shutdown",0)
or use aliases
(gwmi Win32_OperatingSystem).Win32Shutdown(0)
(gwmi win32_operatingsystem -ComputerName MyServer).Win32Shutdown(0)
Parameter -computername – the computer we want to connect to. If the -computername parameter is not present or “.” then the script will retrieve information from the local machine.
Run with different credentials:
(gwmi win32_operatingsystem -ComputerName MyServer -cred (get-credential)).Win32Shutdown(0)
(gwmi win32_operatingsystem).Win32Shutdown(8)
(gwmi win32_operatingsystem).Win32Shutdown(12)
(gwmi win32_operatingsystem).Win32Shutdown(2)
(gwmi win32_operatingsystem).Win32Shutdown(6)
(gwmi win32_operatingsystem -ComputerName MyServer).Win32Shutdown(12)
-computername MyServer. This, of course, is the name of the computer we are interested in.
Run with different credentials:
(gwmi win32_operatingsystem -ComputerName MyServer -cred (get-credential)).Win32Shutdown(12)
(gwmi win32_operatingsystem -ComputerName MyServer).Win32Shutdown(6)
Run with different credentials:
(gwmi win32_operatingsystem -ComputerName MyServer -cred (get-credential)).Win32Shutdown(6)
Jul 25, 2017