1

I am using power shell to find out time when particular process( i.e notepad ) was started and exited.I am using to Get-Process to get status of application but $status.ExitTime does not get time when process was exited. How to get time when process is exited. I am running this in infinite loop so that power shell script does not end.

I have code snippet.

while ('doNotStop') {
$status = get-process notepad -ErrorAction SilentlyContinue
$status | Format-List *
$startTime = $status.StartTime

    if($status)  { 
    Write-Host 'Process is Running' 
    $status.waitforexit()
    Write-Host 'Process startTime='$startTime',endtime='$status.ExitTime 
    }
    else {
    Write-Host 'Process is Not Running' 
    }
}
Raj
  • 37
  • 5
  • An alternative technique to try is [WMI Events](https://stackoverflow.com/a/49438322/9529842). – boxdog Jul 27 '21 at 07:15
  • Add a space before $status.ExitTime `Write-Host 'Process startTime='$startTime',endtime=' $status.ExitTime` – Daniel Jul 27 '21 at 08:00
  • Why not just enable Process Creation and Process Termination auditing, and let Windows take care of the logging process for you automatically? If necessary, you can attach your PowerShell script to the Process Creation and Process Termination events via the Security event log, triggered via WMI -- as boxdog suggests. – leeharvey1 Jul 27 '21 at 13:04

0 Answers0