UPDATED – Fixed issues reporting boottime. 3/20/2012

I was reading Hey, Scripting Guy! article “Calculating Server Uptime” and decided to write a script that can do the same. While the article has very nice script that calculates uptime from event log, my script isn’t fancy and simply calculates uptime since last reboot.

The script can run on any computer with PowerShell 2.0 and will run against local computer or computers specified by list. If you chose to use list, create a text file with one computer name per line. The script header is self explanatory and provides details on how to run the script.

You can download it here – Get-Uptime.ps1

Here’s the script:

#############################################################################
# Get-Uptime.ps1
# This script will report uptime of given computer since last reboot.
# 
# Pre-Requisites: Requires PowerShell 2.0 and WMI access to target computers (admin access).
#
# Usage syntax:
# For local computer where script is being run: .\Get-Uptime.ps1.
# For list of remote computers: .\Get-Uptime.ps1 -ComputerList "c:\temp\computerlist.txt"
#
# Usage Examples:
#
# .\Get-Uptime.ps1 -Computer ComputerName
# .\Get-Uptime.ps1 -ComputerList "c:\temp\computerlist.txt" | Export-Csv uptime-report.csv -NoTypeInformation
#
# Last Modified: 3/20/2012
#
# Created by 
# Bhargav Shukla
# http://blogs.technet.com/bshukla
# https://www.bhargavs.com
# 
# DISCLAIMER
# ==========
# THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE 
# RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
#############################################################################
#Requires -Version 2.0

param 
(
	[Parameter(Position=0,ValuefromPipeline=$true)][string][alias("cn")]$computer,
	[Parameter(Position=1,ValuefromPipeline=$false)][string]$computerlist
)

If (-not ($computer -or $computerlist))
{
	$computers = $Env:COMPUTERNAME
}

If ($computer)
{
	$computers = $computer
}

If ($computerlist)
{
	$computers = Get-Content $computerlist
}

foreach ($computer in $computers) 
{
	$Computerobj = "" | select ComputerName, Uptime, LastReboot
	$wmi = Get-WmiObject -ComputerName $computer -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem"
	$now = Get-Date
	$boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
	$uptime = $now - $boottime
	$d =$uptime.days
	$h =$uptime.hours
	$m =$uptime.Minutes
	$s = $uptime.Seconds
	$Computerobj.ComputerName = $computer
	$Computerobj.Uptime = "$d Days $h Hours $m Min $s Sec"
	$Computerobj.LastReboot = $boottime
	$Computerobj	
}

Originally posted at http://blogs.technet.com/bshukla