Katz
Katz

Reputation: 866

How do you output a process time-span in power shell

I want to be able to output how long a process was running for before it was closed. I was trying to experiment with Where_Object {$_.} but nothing related with time outputs how long the process was running. I was looking at CPU(s) -The amount of processor time that the process has used on all processors, in seconds. I thought this would solve my problem but it doesn't output how long the application was running for. An example of what I'm trying to accomplish would be "If you wanted to know how many seconds you were using Google Chrome today." Here's what I have tried.

 $getchrometime = Get-Process -name chrome| Where-Object {$_.CPU}
    Write-output $getchrometime| Format-Table -AutoSize

Also when I use Format-Table -AutoSize in the ISE the table is not set above it's value.

https://i.sstatic.net/qEp9Y.png

Upvotes: 0

Views: 913

Answers (1)

Kiran Reddy
Kiran Reddy

Reputation: 2904

Get the starttime of all chrome.exe processes(There may be many)

$chrome = Get-Process -Name chrome | select -ExpandProperty starttime

calculate the running time by subtracting the starttime from the currenttime.

$chrome | New-TimeSpan | Select -Expand TotalSeconds

here is a variation that is slightly better

$Chrome = Get-Process -Name chrome
$Chrome |  
    ForEach-Object {
    $_ | Add-Member NoteProperty -Name RunningTime -Value (New-TimeSpan -Start $_.StartTime).TotalSeconds

   }

$Chrome | Select-Object Name,ID,RunningTime

Upvotes: 1

Related Questions