Script to shutdown servers

I run a lot of Microsoft virtual machines in Azure and also locally on my MacBook Pro. These are my lab machines, which I use for testing.

One of the issues with having many virtual machines is orderly shutting them down. It can be a pain to go through each of them and shutting them down.

To circumvent this, I wrote a small PowerShell script, which does it all for me 🙂

This script will shutdown all servers that are in the @serverlist and are active
Date Author Comments
02/06/17 Nivlesh Initial version
$serverlist = @("Server01","Server02","Server03","Server04","Server05")
$server_domainname = "domain.local"
foreach ($server in $serverlist){
$server_fqdn = -join ($server, ".", $server_domainname)
Write-Host "[$server_fqdn]" NoNewline
if (Test-Connection ComputerName $server_fqdn Count 1 ErrorAction SilentlyContinue){
Write-Host ForegroundCOlor Green "Online. Initiating Shutdown" NoNewline
Stop-Computer ComputerName $server_fqdn force
Write-Host ForegroundColor Green " ServerShutdownSuccessfully"
Write-Host ForegroundColor Red "Server is Offline"
Read-Host Prompt "Press Enter when ready to shutdown this host or CTRL+C to Skip"

view raw
hosted with ❤ by GitHub

The script requires the following

$serverlist contains the hostnames of the servers that you want to shutdown (in the order they need to be shutdown)

$server_domainname this is the domain name that the servers are part of.

servername and and server_domainname is used to figure out the server fqdn, which is then used to shutdown that server.

Run the script from a computer that can connect to the servers. Ensure you are logged on as an account that has permissions to shutdown the servers.

The script will go through the list of servers contained in $serverlist and check if they are online. If they are online, then it will try to shut them down.

Do note that these servers will be forced to shutdown, so anything open on those servers will be lost, if not saved.

Once all the online servers have been shutdown, you will be asked if you want to shutdown the computer you are running the script from. You can press Enter to continue or CTRL+C to skip shutting down the computer you are logged on.

Hope this script comes in handy to others