Reputation: 53
A process in windows can be in any of the six states i.e, running, ready, blocked, suspend, new and exit. How to know the state a given process (name, ID) using powershell in windows. In UNIX this information is stored in /proc/$processid/status file. Where is it found in windows or how to get this information in powershell.
Upvotes: 4
Views: 20221
Reputation: 81
You can try this cmdlet to get all available info of a process:
Get-Process -Name <process name> | Format-List *
then you can try this cmdlet to see the state of that process, if it's True means it's running, if false means it's not running
Get-Process -Name <process name> | select Responding
Upvotes: 0
Reputation: 36106
"exit" status is signified by the presense of "exit code" property (natively returned by GetExitCodeProcess()
). In PS, it is reflected by HasExited
and ExitCode
fields in Get-Process
(alias ps
).
ps | where {$_.Id -eq <PID>} | select HasExited,ExitCode
"running/wait/suspended" in Windows is a status of a thread rather than process ("suspend" being one of several Wait substates). I didn't find any info on getting thread information by PS's built-in means but we can call the corresponding .NET functionality:
$process=[System.Diagnostics.Process]::GetProcessById(<PID>)
$threads=$process.Threads
$threads | select Id,ThreadState,WaitReason
Upvotes: 3
Reputation: 4254
You are right, that's an interesting point. A way to find out about the state the process is the following way :
$ProcessActive = Get-Process outlook -ErrorAction SilentlyContinue
if($ProcessActive -eq $null)
{
Write-host "I am not running"
}
else
{
Write-host "I am running"
}
If outlook would not be a running process, it would not be listed but -ErrorAction SilentlyContinue will simply continue and return an I am not running
If it's running it will send you an I am running
I am not aware of other states of a process... at least not how to dertermine
Upvotes: 2