Powershell script to get the uptime of your servers

Sometime you need to find a quick way telling you how long your servers are up.
Here is a little powershell script to get the uptime (in days) of all your servers.

Powershell script:

$servers = Get-ADComputer -prop operatingsystem -Filter {operatingsystem -like "*windows server*"}

$liveservers = @()
$offflineservers = @()
$uptimeservers = @()

foreach ($server in $servers){
if (Test-Connection -ComputerName $server.name -count 1 -ErrorAction SilentlyContinue){
$liveservers += $server}
$offflineservers += $server}

foreach ($server in $liveservers){
$wmi = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $server.name
$days = ($wmi.ConvertToDateTime($wmi.LocalDateTime) – $wmi.ConvertToDateTime($wmi.LastBootUpTime)).days

$server | Add-Member -MemberType NoteProperty -Name Uptime -Value $null -Force

$server.uptime += $days
$uptimeservers += $server

When you have run this code you can just type,

$uptimeservers | sort-object uptime | ft name,uptime -AutoSize

to get a list, sorted by uptime

I find it very handy, I hope you do too.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.