SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

PowerShell – Uptime of Multiple Machines – Example for Custom Formatting and Grid View

This post is to help you out in finding UpTime of one or more computers also you can pass file as its input parameter . Most of the time, the Powershell console might not fit all the required output columns. By default, the ordering of the columns are based on the width of an output data. The best way is to manage it through Custom Formatting or Grid View. This example will show an output in all three formats.

Download the code here Uptime

Function Get-Uptime
Get uptime of a any given server
This function uses Win32_OperatingSystem WMI class to connect to remote machine and get lastboottime
Pass computer name as parameter
Get-Uptime -ComputerName Computername1,computername2
Get-Uptime -ComputerName Computername1
Get-Uptime -ComputerName (cat C:\PowerSQL\WinServers.txt)
To get help:
Get-Help Get-Uptime


[string[]]$ComputerName =”LocalComputer”

$Object =@()
foreach ($Computer in $ComputerName)
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
$Uptime = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer
$LastBootUpTime = $Uptime.ConvertToDateTime($Uptime.LastBootUpTime)
$Time = (Get-Date) – $LastBootUpTime
$Object += New-Object PSObject -Property @{
ComputerName = $Computer.ToUpper();
Uptime = ‘{0:00} Days, {1:00} Hours, {2:00} Minutes, {3:00} Seconds’ -f $Time.Days, $Time.Hours, $Time.Minutes, $Time.Seconds;}


$column1 = @{expression=”ComputerName”; width=15; label=”ComputerName”; alignment=”left”}
$column2 = @{expression=”Uptime”; width=45; label=”Uptime”; alignment=”left”}

# with Customized formatting
$Object|format-table $column1, $column2

#Without formatting
$Object|Format-Table -AutoSize

#Grid output
$Object.GetEnumerator() | Out-GridView -Title ” UpTime Details”




PowerSQL By Prashanth Jayaram

Technology enthusiast with 7+ years of experience in Database Technology. I am Microsoft Certified Professional with certificates of OCA, MCP, MCTS, MCITP developer, MCITP administration and backed with a degree in Master of Computer Application. My expertise lies in T-SQL programming, Replication, PowerShell and Performance Tuning. Hobbies are Drawing, playing soccer and listening to Melodies songs.


Leave a comment on the original post [sqlpowershell.wordpress.com, opens in a new window]

Loading comments...