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.
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.
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!
SolarWinds Firewall Browser
Here is an utility where you can review firewall settings such as
access control lists (ACL), or troubleshoot problems with network
address translation (NAT).
Other reasons to download this SolarWinds Firewall Browser include
managing requests to change your firewall settings, testing firewall
rules before you go live, and querying settings with the browser's
powerful search options.
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 v10
This
Engineer's Toolset v10 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 now!
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
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.
Windows Management Instrumentation (WMI) is
most useful for PowerShell scripting.
SolarWinds
have produced this
Free WMI Monitor to take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.