Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'(get-computerinfo).osuptime' change output format

Can the output of the PowerShell command (get-computerinfo).osuptime be changed?

enter image description here

This how is currently displays by default.

Is there a way to make it something more like the following format: dd:hh:mm:ss

I would like do do this using a .ps1 so I can just run it, check the uptime in that format, and close the PowerShell window.

like image 932
Brandon Bronkhorst Avatar asked Feb 26 '26 04:02

Brandon Bronkhorst


1 Answers

You can use the TimeSpan.ToString method to get that desired format, note that escaping of : with \ is needed.

(Get-ComputerInfo).OsUptime.ToString('dd\:hh\:mm\:ss')

Alternatively, the G format might work for you (it's not exactly the same as it includes the fractional seconds too), see The General Long ("G") Format Specifier for reference.

(Get-ComputerInfo).OsUptime.ToString('G')

Another way to get the uptime that is probably more performant is to query Win32_OperatingSystem and substract DateTime.Now from the LastBootUpTime property:

$timespan = [datetime]::Now - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
$timespan.ToString('dd\:hh\:mm\:ss')
like image 169
Santiago Squarzon Avatar answered Feb 27 '26 19:02

Santiago Squarzon