posh | op Computer

http://www.computerperformance.co.uk/powershell/powershell_restart_computer.htm
http://www.computerperformance.co.uk/powershell/powershell_stop_computer.htm

Simple Example of Restart-Computer

For once it's not easy to test this command on the local computer, unless you add the -confirm switch.  As I will explain later, if you specify a network computer with Restart-Computer you are probably going to have to disable the firewalls.

# Use PowerShell to reboot a network machine
Restart-Computer -computerName LittleServer

Example of Restart-Computer on Multiple Servers

# PowerShell Reboot Multiple Servers
$Victims ="BigServer, LittleServer, GnomeServer"
Restart-Computer -computer $Victims -force

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


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.


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 restrictionson 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!

Comments