Public/Get-SystemUptime.ps1
|
function Get-SystemUptime { <# .SYNOPSIS Returns how long the system has been running since last boot. .DESCRIPTION Wraps Get-CimInstance Win32_OperatingSystem to calculate uptime as both a TimeSpan object and a friendly formatted string. Windows-only. .EXAMPLE Get-SystemUptime Returns last boot time, uptime as TimeSpan, and a friendly string. #> [CmdletBinding()] param () if (-not $IsWindows -and $PSVersionTable.PSEdition -eq 'Core') { throw "Get-SystemUptime currently supports Windows only." } $os = Get-CimInstance -ClassName Win32_OperatingSystem $lastBoot = $os.LastBootUpTime $uptime = (Get-Date) - $lastBoot [PSCustomObject]@{ LastBootTime = $lastBoot Uptime = $uptime UptimeFriendly = "{0}d {1}h {2}m" -f $uptime.Days, $uptime.Hours, $uptime.Minutes } } |