Trigger automatic Shutdown on Windows Server in case of power failure

Publish Date: August 29, 2018

We have a Windows server 2012 R2 with Mirroring (RAID 1) configured which will get corrupted if it gets Shutdown unexpectedly. Most of the time IT staff is there to look after the server but on Sunday, there is nobody in Office to manually shut down the server in case of Power outage. We needed a way to automatically trigger the shutdown in that case.

On Googling and reading some articles, I found that some UPS vendors (such as APC) provide some utility along with their UPS. But those will only work if your UPS is network enabled. Sadly my UPS is neither APC nor it is network enabled. So, I thought of creating a solution of my own which is not 100% perfect but will work for my problem.

I have a laser Printer in Office which always remains On and since it is a Laser printer, it is not connected to UPS. It is directly plugged into main Power. The printer is network enabled and has a static IP. So I thought of creating a PowerShell script which will keep polling the printer from my server after a regular intervals. If it does not get a reply, it is most likely due to failed power. I know this is not 100% correct and fool-proof solution but it works for me. You could use the similar technique; maybe using a different device to ping such as CCTV DVR or something like that.

Following is the PowerShell script:

while ($true) {
if (-not (Test-Connection '' -Quiet)) {
Write-Host "Connection failed at $(Get-Date -Format "dd/MM/yyyy hh:mm:ss tt")" -ForegroundColor Red
Write-Host "Initiating emergency Shutdown" -ForegroundColor Red
msg * "Initiating emergency Shutdown"
Start-Sleep -Seconds 30
if ([System.Diagnostics.EventLog]::SourceExists("Emergency Shutdown") -eq $False) {
New-EventLog -LogName "System" -Source "Emergency Shutdown"
Write-EventLog -LogName System -Source "Emergency Shutdown" -EntryType Error -EventId 1 -Message "Emergency shutdown triggered due to Power failure"
Stop-Computer -Force 
exit 0
else {
Write-Host "Connection successful at $(Get-Date -Format "dd/MM/yyyy hh:mm:ss tt")" -ForegroundColor Green
Start-Sleep -Seconds 300

Next step is to save the script to a location like D:\Scripts\Invoke-EmergencyShutdown.ps1 and schedule this script with custom trigger to Run only on Sunday.

The script will keep checking the connection to Printer ( every 5 minutes. If it doesn’t get a response, it will throw a system wide message on screen “Initiating emergency Shutdown“. Then it also writes an entry in System log with custom Source “Emergency Shutdown” so that you could track the time of Shutdown. After making a log entry it will issue the Stop-Computer command and server will shutdown.

Following image shows the Log entry created by the script


I know it is not a 100% correct solution but it serves my purpose. That is why I shared it here. Maybe it could help someone else.


Microsoft Certified | Cisco Certified