PowerShell – Log Off and Shutdown

email me

Use WMI in PowerShell to control log off and shutdown. Useful for when logoff.exe or shutdown.exe will not work (as in virtual sessions).

 

0 – Logoff

(Get-WmiObject -Class Win32_OperatingSystem).Win32Shutdown(0)

1 – Shutdown

(Get-WmiObject -Class Win32_OperatingSystem).Win32Shutdown(1)

2 – Reboot

(Get-WmiObject -Class Win32_OperatingSystem).Win32Shutdown(2)

4 – Forced logoff

(Get-WmiObject -Class Win32_OperatingSystem).Win32Shutdown(4)

5 – Forced shutdown

(Get-WmiObject -Class Win32_OperatingSystem).Win32Shutdown(5)

6 – Forced reboot

(Get-WmiObject -Class Win32_OperatingSystem).Win32Shutdown(6)

8 – Power off

(Get-WmiObject -Class Win32_OperatingSystem).Win32Shutdown(8)

12 – Forced power off

(Get-WmiObject -Class Win32_OperatingSystem).Win32Shutdown(12)

 

Notes

Win32Shutdown method of the Win32_OperatingSystem class

logoff.exe

Logoff sessionID /server:ComputerName

 

Scheduled Shutdown

$action = New-ScheduledTaskAction -Execute ‘Powershell.exe’ -Argument ‘-NoProfile -WindowStyle Hidden -command “& {stop-computer}”‘
$trigger = New-ScheduledTaskTrigger -Daily -At 9pm
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName “Shutdown” -Description “Daily shutdown”