I was looking for a script or program that would automatically notify me when my Windows computer reboots and I came up empty. The reason I wanted it? I have a Windows Server and I have noticed that for whatever reason it was rebooting and I had no way in knowing if it was rebooted. So, I was able to write my own script using Windows PowerShell and the Task Scheduler. If you don’t already have Windows PowerShell installed then you can download PowerShell 2.0 (for Windows Server 2003, Windows Server 2008 SP1, Windows XP, and Windows Vista) or PowerShell 3.0 (for Windows 7 SP1, Windows Server 2008 R2 SP1, and Windows Server 2008 SP2).

Once you have Windows Powershell installed (if it wasn’t already), then the first thing your going to want to do is allow self-written scripts to be executed. To do this, open Powershell by either going to Start -> Programs -> Accessories -> Windows PowerShell -> Windows PowerShell or opening a command prompt and entering “powershell.exe”. Now your going to enter this into PowerShell:

[highlight lang=”powershell”]Set-ExecutionPolicy Unrestricted[/highlight]

(NOTE: This is not recommended, however, unless you want to go through the hassle of signing the script then its the only way this is going to work)

The next step is to configure the PowerShell script so it will automatically send you an e-mail. You will need an SMTP server to use in order for the e-mails to work. Here’s the PowerShell code:

[highlight lang=”powershell”]Write-Host “Sending Email”

#SMTP server name
$smtpServer = “smtp.xxxx.com”

#SMTP username
$smtpUser = “myusername”

#SMTP password
$smtpPass = “mypassword”

#From email
$msgFrom = “nobody@windows.com”

#To email
$msgTo = “myemail@mydomain.com”

#Email subject
$msgSubject = “Server Rebooted”

#Email body
$msgBody = “It appears that your Windows Server has been rebooted”

#Creating a Mail object
$msg = new-object Net.Mail.MailMessage

#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$smtpcred = new-object System.Net.networkCredential
$smtpcred.username = $smtpUser
$smtpcred.password = $smtpPass
$smtp.Credentials = $smtpcred

#Email structure
$msg.From = $msgFrom
$msg.subject = $msgSubject
$msg.body = $msgBody

#Sending email

You will need to save this code to a file with the extension .ps1. For this example, I am going to have it saved as notify.ps1 on the C: drive.

Next, your going to have to tell Windows to execute this script every time it is booted. So, we’re going to create a scheduled task to have it executed. To do this, open up the command prompt and enter the following command:

[highlight lang=”dos”]schtasks /create /tn “Boot Notify” /tr “powershell.exe -File “c:notify.ps1″” /sc ONSTART /ru system[/highlight]

You will probably have to replace c:notify.ps1 with the correct path to where your script is located on your hard drive. I added the slashes before the quotes so you can use a path that has spaces in it.

The only thing left to do now is to test it out by rebooting your computer and you should have received an email in your inbox. If it didn’t work then you can try executing the PowerShell script and seeing if any errors occurred, or, try executing the command given to the scheduled task (powershell.exe -File “c:notify.ps1”).

If you have any questions, please leave a comment below. I hope you enjoyed this tutorial!