Skip to content

Instantly share code, notes, and snippets.

@fheck
fheck / runtime.ps1
Last active May 5, 2019 22:56
Informative "uptime" function for powershell on Windows
function runtime {
function extractDate($text, $type) {
$regex = '.*?' + $type + ' Time: (?:.?(\d{4}).?-.?([01]\d).?-.?([0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+(?:[+-][0-2]\d:[0-5]\d|Z))).*?'
$date = ( % {[regex]::Match($text, $regex)}).Groups
return Get-Date($date[1].Value + "-" + $date[2].Value + "-" + $date[3].Value)
}
$now = Get-Date
$bootevents = Get-WinEvent -FilterHashtable @{logname="system";providername="Microsoft-Windows-Kernel-Boot";ID=27} -ErrorAction SilentlyContinue | Sort-Object -Property "Timecreated" -Descending
$boottime = ($bootevents | Where-Object {$_.message -notlike "*0x2."} | Select -first 1).Timecreated