Windows server UPTime Report Using PowerShell

Posted on: November 16, 2012

If you are a windows server administrator then , one or the other day you will be asked to get the UPTime report of list of windows servers (Most of the times your Boss will ask this :))

Below Script will help you to get the UPtime report of listed servers

Before you run the script ,

  • Create a text file servers.txt in C:\ Drive and list the servers against which you need to run this query (The Script will list out the UPtime report of listed server)

Description –

This script will make use of WMI Query (Class – win32_Operatingsystem) and get the lastboottime of listed server. Script will produce 2 TXT Files namely , UPtimeReport.txt and Offline.txt in your C:\ Drive

Note – If all the servers are online then offline.txt will not be created and Vice Versa . i.e if all the servers are offline then UPTimeReport.txt will not be created.

PowerShell Script

#Title – Script for querying windows server UPTime/Lastboot

#Description – This script queries windows servers listed in Servers.txt file (WMI Query) and get their last bootuptime

$servers = Get-Content C:\Servers.txt
foreach ($s in $servers)
$a=Get-WmiObject -ComputerName $s -Class Win32_OperatingSystem -ErrorAction ‘Stop’
$b = $a.convertToDateTime($a.Lastbootuptime)
[TimeSpan]$LastBoot = New-TimeSpan $b $(Get-Date)
(‘{0} {1}Day(s),{2}Hour(s),{3}Min(s),{4}Seconds’ -f $s,$LastBoot.Days,$lastboot.Hours,$LastBoot.Minutes,
$LastBoot.Seconds) | out-file C:\UptimeReport.txt -append -Encoding ascii
(‘{0} Server is notreachable’ -f $s)| Out-File C:\offline.txt -Append -Encoding ascii

I Hope this script will help , and it will make your BOSS Happy as well 🙂




5 Responses to "Windows server UPTime Report Using PowerShell"

What’s up to all, as I am actually keen of reading this weblog’s post to be updated
on a regular basis. It contains good material.

Are you able to modify the report to show servers that have an uptime of less than 1 day?

Yes , that can be done , let me know you exact requirement , I will modify the send the script to your email

Thank you. here’s what i need. A report of servers that rebooted within the last 24 hours and by whom/service. If the by whom part is tricky then just the script to see the last 24 hours reboots.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: