Get-SystemUptime
Windows: Calculates the system uptime and last boot time
#Requires -Version 5.1
[CmdletBinding()]
Param
(
[string]$ComputerName = $env:COMPUTERNAME,
[pscredential]$Credential
)
Process
{
try
{
$session = $null
$cimParams = @{
'ClassName' = 'Win32_OperatingSystem'
'ErrorAction' = 'Stop'
}
if ($ComputerName -ne $env:COMPUTERNAME)
{
$sessionParams = @{
'ComputerName' = $ComputerName
}
if ($null -ne $Credential)
{
$sessionParams.Add('Credential', $Credential)
}
$session = New-CimSession @sessionParams
$cimParams.Add('CimSession', $session)
}
$os = Get-CimInstance @cimParams | Select-Object -First 1
$bootTime = $os.LastBootUpTime
$uptime = New-TimeSpan -Start $bootTime -End (Get-Date)
$result = [PSCustomObject]@{
LastBootTime = $bootTime
UptimeDays = $uptime.Days
UptimeHours = $uptime.Hours
UptimeMinutes = $uptime.Minutes
UptimeString = "$($uptime.Days) Days, $($uptime.Hours) Hours, $($uptime.Minutes) Minutes"
ComputerName = $ComputerName
}
Write-Output $result
}
catch
{
throw
}
finally
{
if ($null -ne $session)
{
Remove-CimSession $session
}
}
}Specifies the name of the computer to query. Defaults to the local computer.
Specifies a PSCredential object for remote connection.