Can the output of the PowerShell command (get-computerinfo).osuptime
be changed?
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.
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')