PowerShell System Uptime

I just needed to know how long it was since my box was last rebooted. Windows Vista has a nice feature where this is now a property of the Performance tab in Task Manager. But alas, there’s no such nice functionality in Windows 2K3. Usually I end up in PerfMon to get the number of seconds the box has been up and then in calculator to convert this into real time.

Enter PowerShell to the rescue:

function GLOBAL:Get-SystemBootupTime([String]$computer=”localhost”) {
  $wmiOsInformation = Get-WmiObject -computer $computer -class Win32_OperatingSystem
  $wmiOsInformation.ConvertToDateTime($wmiOsInformation.LastBootUpTime)
}

function GLOBAL:Get-SystemUptime([String]$computer=”localhost”) {
  $wmiPerfOsSystem = Get-WmiObject -computer $computer -class Win32_PerfFormattedData_PerfOS_System
  $wmiPerfOsSystem.SystemUpTIme
}

# Get System Bootup time
PS C:\> Get-SystemBootupTime
21 December 2006 17:16:02

# Get system uptime in seconds
PS C:\> Get-SystemUptime
1016328

Not bad, I can now see the system uptime in seconds, but I want the result formatted

function GLOBAL:Get-SystemUptimeFormatted([String]$computer=”localhost”) {
  $wmiPerfOsSystem = Get-WmiObject -computer $computer -class Win32_PerfFormattedData_PerfOS_System
[TimeSpan] $systemUptime = New-TimeSpan -seconds $wmiPerfOsSystem.SystemUpTIme
  [String]::Format(”{0:G}”, $systemUptime)
}

PS C:\> Get-SystemUptimeFormatted
11.18:53:16

Job done! As a developer, I tend towards using .NET all over, so I’ve probably missed some rather subtle ways of achieving the formatting with PowerShell.

You can get a lot of other useful information from the WMI classes that could be the focus of other functions:

PS C:\> $computer = “localhost”
PS C:\> $wmiOsInformation = Get-WmiObject -computer $computer -class Win32_OperatingSystem
PS C:\> $wmiOsInformation SystemDirectory : C:\WINDOWS\system32
Organization : My Org
BuildNumber : 3790
RegisteredUser : Me
SerialNumber : xxxxx-xxx-xxxxxxx-xxxxx
Version : 5.2.3790

To ensure that this handy set of functions is always there when needed, I’ve added them to my profile:

PS C:\> Notepad $profile

4 Responses to “PowerShell System Uptime”

  1. > I’ve probably missed some rather subtle ways of achieving the formatting with PowerShell.

    in PowerShell you can use the format operator ( -F ) as a shortcut :

    PoSH>”{0:G}” -f $systemUptime
    11.18:18:48

    Greetings /\/\o\/\/

  2. in this case as G is the default we can do this even shorter :

    PoSH>”$systemuptime”
    11.18:18:48

    Greetings /\/\o\/\/

  3. Sorry, if I’m spamming, but you can also run the command in the string like this :

    “$(New-TimeSpan -seconds $wmiPerfOsSystem.SystemUpTIme)”

  4. A little concoction of my own:
    >[string]$systemuptime.Days+’.'+[string]$systemuptime.Hours+’:'+[string]$systemuptime.Minutes+”:”+[string]$systemuptime.Seconds

    I know, it’s a looong line… But gives way to a bit more extensive formatting (at least, I think so).
    Try this for some clarification:
    >[timespan]$test = 0
    >$test

    Just an observation:
    >[String]::Format(”{0}”, $systemUptime)
    is equivalent in this case with
    >[String]::Format(”{0:G}”, $systemUptime)

    I’ve tried other format specifiers besides “G” and I got the same result.

    Very useful post, Mr. Sheppard. Keep posting!

Leave a Reply