Search code examples
powershellpid

How does one obtain the pid of a process started from powershell?


If I use powershell to run an external command, how do I obtain it's pid?

I'd like to obtain it for purposes of gracefully exiting it later (not just straight up killing the process, but rather doing it in more the sense of a finally block)


Solution

  • Using System.Diagnostics.Process object, you can start a process, and return a Process object, which includes the newly created process PID.

    For example:

    $Process = [Diagnostics.Process]::Start("nssm.exe")   
    Write-Host $Process.Id