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”




About Prashanth Jayaram

DB Technologist, Author, Blogger, Service Delivery Manager at CTS, Automation Expert, Technet WIKI Ninja, MVB and Powershell Geek My Profile: https://social.technet.microsoft.com/profile/prashanth jayaram/ http://www.sqlshack.com/author/prashanth/ http://codingsight.com/author/prashanthjayaram/ https://www.red-gate.com/simple-talk/author/prashanthjayaram/ http://www.sqlservercentral.com/blogs/powersql-by-prashanth-jayaram/ Connect Me: Twitter @prashantjayaram GMAIL powershellsql@gmail.com The articles are published in: http://www.ssas-info.com/analysis-services-articles/ http://db-pub.com/ http://www.sswug.org/sswugresearch/community/
This entry was posted in PowerShell and tagged , , . Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s