Daniel Paczuski Bak
Daniel Paczuski Bak

Reputation: 4080

Track PID of process I just started in PowerShell

casperjs CMQ_scraper.js
Stop-Process -name phantomjs

I want to start a process (casperjs CMQ_scraper.js) and then immediately kill it.

CMQ_scraper.js shows up as phantomjs.exe in command line. however, I may have other phantomjs instances running that I don't want to kill. How can I save the PID of the new process so that I can later kill it?

Upvotes: 1

Views: 3236

Answers (1)

Matt
Matt

Reputation: 46710

Use Start-Process to create your process and use the -PassThru switch. That way you can save the return into a variable.

Then you can use that variable and pipe it into Stop-Process

$specificProcess = Start-Process notepad.exe -PassThru
$specificProcess | Stop-Process

As Ansgar points out you can call the .Kill() method as well of $specificProcess

$specificProcess.Kill()

If you really needed the PID then you can get that off the $specificProcess.ID

Upvotes: 4

Related Questions