Shutdown (Power off) or Reboot a Remote Computer from Powershell

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.

Win32Shutdown Method of the Win32_OperatingSystem Class

uint32 Win32Shutdown( sint32 Flags, sint32 Reserved );

Parameters

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.

Return Values

Returns zero (0) to indicate success. Any other number indicates an error.

Logging Off the Current Session from Powershell

Logging off the local computer

(Get-WmiObject -Class Win32_OperatingSystem -ComputerName .).InvokeMethod("Win32Shutdown",0)

or use aliases

(gwmi Win32_OperatingSystem).Win32Shutdown(0)

Logging off the remote computer

(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) 

Shutting Down or Rebooting a Computer from Powershell

Shutdown (Power off) a local computer

(gwmi win32_operatingsystem).Win32Shutdown(8)

Force shutdown

(gwmi win32_operatingsystem).Win32Shutdown(12)

Reboot a local computer

(gwmi win32_operatingsystem).Win32Shutdown(2)

Force reboot a local computer

(gwmi win32_operatingsystem).Win32Shutdown(6)

Shutdown (Power off) a remote computer

(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) 

Reboot a remote computer

(gwmi win32_operatingsystem -ComputerName MyServer).Win32Shutdown(6) 

Run with different credentials:

(gwmi win32_operatingsystem -ComputerName MyServer -cred (get-credential)).Win32Shutdown(6) 

Related links

Jul 25, 2017

Similar articles