Restarting with Restart-Computer
A cmdlet that will have you whistling while you work.
- By Jeffery Hicks
I'm not the biggest Rolling Stones fan, but I always liked Start Me Up, which by the way was a Windows 95 launch song. But now that we're in the 21st century, PowerShell has us singing a new tune. Hopefully you're finding it catchy. If not, then maybe today's lesson will provide the harmonic overtones you've been seeking.
It's a necessary evil that Windows-based servers need to be periodically restarted. To do this remotely in the past we relied on command-line resource kit tools or Window Management Instrumentation (WMI) based scripts.
Even in PowerShell 1.0 we could use Get-WMIObject to connect to the Win32_OperatingSystem class. This class has a Reboot() method. You could execute the method to remotely reboot a computer, although it took an extra step that wasn't well documented. But now PowerShell 2.0 gives us a cmdlet that makes this management task very, very easy:
PS C:\> help restart-computer
This cmdlet can be used to restart the local or a remote computer. You don't even need PowerShell 2.0 installed on the remote computer or even use the remoting feature unless you want to run the task as a background job. I don't see a need for that unless you are attempting to restart more than a handful of remote computers at once.
Rebooting a few servers is as easy as this:
PS C:\>restart-computer -computername PRINT01,APP02,FILE01
All I need to do is specify a comma-separated list of server names. Or if you prefer to run through a text list of computer names, then do this:
PS C:\> restart-computer -ComputerName (get-content \\ITSRV\files\servers.txt) -force
The cmdlet also has a -Force parameter you might need to use to force logoff of any existing sessions and force the machine to reboot. Because rebooting a server is serious ju-ju you might want to use the -Whatif and -Confirm parameters to make sure you're only rebooting what you intend.
Even though we have a handy cmdlet to use, it still relies on WMI and the Win32_OperatingSystem class to do the heavy lifting. The upside is that we can specify a PSCredential for alternate credential authentication. This also means no dependencies on WinRM. The downside is that you need RPC connectivity to the remote computer for this expression to complete successfully. But for small networks this typically isn't much of an obstacle.
You can incorporate Restart-Computer into your existing PowerShell management scripts and start singing a new tune today.
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.