PowerShell Basics: Shutdown Command – Stop-Computer

PowerShell Basics_ Shutdown Command Stop-Computer

PowerShell Stop-Computer Cmdlet

If you want to use PowerShell to shutdown your machine, choose the Stop-Computer cmdlet.  The PowerShell Stop-Computer cmdlet is very similar to the operating system’s built-in shutdown command.  A likely scenario is that you wish to down a remote server.

Topics for PowerShell’s Shutdown Command

Simple Example of Stop-Computer

# PowerShell shutdown example
Clear-Host
Stop-Computer -computerName ExchServer

Note 1: This command shuts down one named computer called ExchServer.

Example of Stop-Computer on Multiple Servers

$Victims =”SQLServer, ExchServer, GnomeServer”
Stop-Computer -comp $Victims -force

Note 2: This example shuts down multiple computers, the names of which are stored in a variable called $Victims.  You could extend this idea by employing Get-Content to read the names of the servers stored in a text file.  Incidentally, I have shortened the parameter -computerName to -comp, in PowerShell you can shorten parameters so long as the truncated word is unambiguous.

Stop-Computer – Basics of Shutdown

If you want to use PowerShell to shutdown your machine, choose the Stop-Computer cmdlet.  However, let us start by investigating PowerShell cmdlets containing the noun ‘computer’.  If you don’t see Stop-Computer among the results this will remind you to get the latest version of PowerShell v 2.0.

# Results should include, stop, restart and add.
# Else, try $Host to check you have PowerShell v 2.0.
Get-Command -Noun computer

Researching Stop-Computer Cmdlet

Before we stop our first computer using a PowerShell command, it’s well worth calling for help so that we can check the syntax and examine the parameters for Stop-Computer.

Clear-Host
Get-Help Stop-Computer -full

Note 3:  I like to append the -full switch so that I can see the examples.  Two interesting parameters revealed with help are -force and -credential.  It’s also worth noting that Stop-Computer uses WMI, hence there are possible firewall restrictions on this command.

Note 4:  In the case of Stop-Computer, I can see many opportunities to add the -confirm switch.  Admittedly I reached this conclusion only after I had shot myself in the foot, and downed my local machine instead of the network server I was aiming at!

Guy Recommends:  Network Performance Monitor (FREE TRIAL)Review of Orion NPM v11.5

SolarWinds Network Performance Monitor (NPM) will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM on a 30-day free trial.

SolarWinds Network Performance Monitor Download 30-day FREE Trial

Alternative to Stop: Restart-Computer

Perhaps you just want to restart the computer after apply a patch; in which case substitute ‘Restart’ for ‘Stop’, thus:

# Use PowerShell to reboot your server
Restart-Computer -computerName YourServer

Note 5: Once again the firewall may prevent the command, if so try turning off the firewall, or better still make an exception for WMI.

See more on PowerShell’s Restart-Computer »

Problems with Stop-Computer

RPC Server is unavailable

  • Error Message: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
  • Cause: Firewall blocking the WMI / RPC command.
  • Solution: Turn off the firewalls, alternatively, open just the RPC ports 135 and 445.

Note 5: For this task I often use the local policy editor Gpedit.msc.  Then go to Windows Settings, Security Settings, Windows Firewall.  From that menu I right-click and create a New Rule seeking out ‘Predefined’, then scrolling down to WMI…

Access is Denied

  • Error Message: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
  • Solution: append the -credential parameter, use the format MachineName\UserName

# PowerShell’s Stop-Computer with the credential parameter
Stop-Computer -computerName Serv1 -credential Serv1\Username

Guy Recommends: SolarWinds Engineer’s Toolset (FREE TRIAL)Engineer's Toolset v10

This Engineer’s Toolset provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset on a 14-day free trial now!

SolarWinds Engineer's Toolset Download 14-day FREE Trial

Research More ‘Computer’ Cmdlets

Let us investigate which other PowerShell cmdlets contain the noun ‘computer’.

Get-Command -Noun computer

# Results for PowerShell 3.0

Name
—————
Add-Computer
Checkpoint-Computer
Remove-Computer
Rename-Computer
Restart-Computer
Restore-Computer
Stop-Computer

See also Restart-Computer »

Summary of Windows PowerShell Stop-Computer Cmdlet

Stop-Computer is very similar to the old shutdown command which has been built-in to generations of Windows operating systems.  The advantage of Stop-Computer is that it’s simpler than remembering whether you need a switch such as -s or -r, furthermore the backslashes can be troublesome with the shutdown command.

If you like this page then please share it with your friends


See more PowerShell examples for Shutdown commands

PowerShell Home   • Syntax   • Stop-Computer   • Restart Computer   • Free CSV Import Tool

Get-Credential   • Windows PowerShell   • Windows 8 PowerShell 3.0

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.