How to Shut Down, Restart and Log Off Windows Using PowerShell

Introduction

PowerShell enables IT Admin or Software Engineer to automate shutting down, restarting or logging off Windows computer. In this blog post, we will walk through how to achieve those tasks using PowerShell.

Solution

Using Stop-Computer and Restart-Computer Cmdlet

These commands are used only for shutting down and restarting windows. There is no counterpart for logging off computer.

To shut down computer, you can use below cmdlet:


Stop-Computer

To restart computer, you can use below cmdlet:


Restart-Computer

Using method from Get-WmiObject

To log off computer, you can use below command:


Get-WmiObject -Class Win32_OperatingSystem | Invoke-WmiMethod -Name Win32Shutdown -Argument 0

To shut down computer, you can use below command:


Get-WmiObject -Class Win32_OperatingSystem | Invoke-WmiMethod -Name Win32Shutdown -Argument 1

To restart computer, you can use below command:


Get-WmiObject -Class Win32_OperatingSystem | Invoke-WmiMethod -Name Win32Shutdown -Argument 2

Using Command Prompt shutdown command

Since we can run Windows Command Prompt commands from PowerShell, we can also utilize its commands.

To shut down computer, you can use below command:


shutdown /s

To restart computer, you can use below command:


shutdown /r

To log off computer, you can use below command:


logoff

Conclusion

In conclusion, to shut down, restart or log off Windows computer, we can use PowerShell commands like Stop-Computer and Restart-Computer. We can also utilize method from Get-WmiObject. Besides that, we can also use shutdown or logoff command with its options which is borrowed from Windows Command Prompt.