Prof. Powershell

How To Restart Computers Remotely via PowerShell

The trick is easy with the aptly named Restart-Computer

It is a fact that Windows administrators periodically need to reboot servers and desktops. Because of this fact, I always was running into scripts that would remotely reboot a group of computers. But in PowerShell, this is now (dangerously) easy and no scripting is required. All we need is the Restart-Computer cmdlet.

You can use the cmdlet to restart your own computer without much hassle:

PS C:\> Restart-Computer

To restart a remote computer, you need to run the command with credentials that have the right privileges to remotely shut down a computer, typically an admin account. Assuming my current credentials were adequate, I could easily reboot the computer SERVER01:

PS C:\> Restart-Computer Server01 -whatif

What if: Performing operation "Restart-Computer" on Target " (Server01)".

although I didn't really; I took advantage of the -WhatIf parameter to verify my command. This is especially helpful if I'm rebooting a bunch of servers:

PS C:\> restart-computer "server01","server02","server03" -whatif

By the way, here's how you could shut down a list of computers:

PS C:\> restart-computer (get-content c:\work\computers.txt)

Because the cmdlet is using WMI objects and methods under the hood you can specify alternate credentials, either a saved credential object or a user name:

PS C:\> restart-computer (get-content c:\work\computers.txt) -credential "mycompany\administrator"

I'll get prompted for the password, but then this credential will be used for every computer in the list.

The Restart-Computer cmdlet will fail, if a logon session is detected. PowerShell will raise an exception. However, you can force a reboot using -- what else? -- the -force parameter. Be aware this will force applications to close with the potential loss of unsaved work.

Another option for rebooting or even logging off is to use the Win32_OperatingSystem WMI Class and the Win32ShutDown method. I recommend using Invoke-WMIMethod because it supports -WhatIf and -Confirm. I found it just as easy to pipe a WMI object from Get-WMIObject to Invoke-WMIMethod:

PS C:\> Get-WmiObject win32_operatingsystem -ComputerName Quark | Invoke-WMIMethod -name Win32Shutdown

The Win32Shutdown method can accept parameters. The default is 0 which means do a simple logoff. But if the user has open files or if the default method fails, you can always resort to a forceful logoff:

PS C:\> Get-WmiObject win32_operatingsystem -ComputerName Quark | Invoke-WMIMethod -name Win32Shutdown -ArgumentList @(4)

You have to specify the value for -ArgumentList as an explicit array. If you look at the MSDN documentation for this method you'll discover other method parameter values, which you can certainly use. But personally many of them are duplicated with cmdlets like Restart-Computer and Stop-Computer; given a choice I prefer to use a cmdlet.

I hope it goes without saying that this is pretty powerful ju-ju so be sure you've tested your scripts and tried out these commands in a non-production environment.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular