Stephen Sugumar
Stephen Sugumar

Reputation: 545

Powershell extract Time property from Variable

I have this Query:

$cpuTime = (get-wmiobject Win32_Process -filter "commandline like '%STRING%'" -computername $server)
$cpuTime | % { $_.ConvertToDateTime( $_.CreationDate )}

How do I extract just the "time" (hh,mm,ss) from the $cpuTime variable?

Upvotes: 1

Views: 1561

Answers (1)

dugas
dugas

Reputation: 12443

If you just want a string, you can call ToString with a custom date and time format string.

$x = (get-wmiobject Win32_Process )
$x | % { $creationDate = $_.ConvertToDateTime( $_.CreationDate ); $creationDate.ToString("hh:mm:ss")}

You could call DateTime.TimeOfDay to get a Timspan back that represents elapsed time since midnight.

$x = (get-wmiobject Win32_Process )
$x | % { $creationDate = $_.ConvertToDateTime( $_.CreationDate ); $creationDate.TimeOfDay} |
Select-Object Hours, Minutes, Seconds

Upvotes: 1

Related Questions