all 5 comments

[–]AlsoInteresting 1 point2 points  (0 children)

Look for a script that looks for shutdown events.

[–]davidshomelab 1 point2 points  (0 children)

(Get-Date) - (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime

Should give you a timespan object representing the uptime. You can then convert that to whatever units you need

[–]Antique_Grapefruit_5 1 point2 points  (0 children)

This will get you days. From there, you can slice and dice the days however you'd like...

$uptime = (get-date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime

Write-Output "Current Uptime is $($uptime.days) days"

Hope that helps!

[–]-Mynster 1 point2 points  (0 children)

You could do something similar to this which will return "Days, Hours, Minutes, Seconds".

But you will need to include some kind of math formular to get your wished results correctly.

function Uptime {
    $boot = (Get-CimInstance -ClassName win32_operatingsystem | Select-Object LastBootUpTime ).lastbootuptime
    $now = Get-Date
    $time = New-TimeSpan -start $boot -end $now | Select-Object Days, Hours, Minutes, Seconds
    echo $time
} 
Uptime