InfotechGuyz.com Blog

Powershell Script to Shutdown List of Computers

I’ve written a Powershell script that shutdowns list remote computers from a list which is a text file. This Powershell script use WMI to shutdown the remote system.

1. Open notepad

2. Copy and paste below text to notepad

3. Save the file with .ps1 extension.

USAGE: c:\shutdownremotely.ps1 c:\shutdownlist.txt

# ---------- SCRIPT STARTS HERE--------------

$inputfile = Dir $args[0]
$readinputfile = get-content $inputfile
$readinputfile | ForEach-Object {

$objsystem = get-wmiobject -computername $_ -query "select * from win32_operatingsystem"
$result = $objsystem.shutdown()

if ($result.returnvalue -match "0") {
write-host "$_ - shutdown command completed" -foregroundcolor green
}
else {
write-host "$_ - unable to send shutdown command" -foregroundcolor red
}

}

# ---------- SCRIPT ENDSS HERE--------------