user2437585
user2437585

Reputation: 19

powershell how ProcessID based on CommandLine match

I want to get process ID based on process commandline.

$saucelab = gwmi Win32_Process -Filter "name = 'java.exe'" | select CommandLine, ProcessID

Now there could be many process with Name "java" but I want to find process of the specific process that contains my string.

$pid = $saucelab | Where-Object {$_.CommandLine -contains "-port 4444"} | select ProcessID

This does not work

Is there any way to get processID based on process commandLine match

Upvotes: 1

Views: 2111

Answers (1)

mjolinor
mjolinor

Reputation: 68341

-contains is an array operator. Commandline will be a string. Try using -match instead:

$pid = $saucelab | Where-Object {$_.CommandLine -match "-port 4444"} | 
select -ExpandProperty ProcessID

see:

 Get-Help about_comparison_operators 

Edit: When you select one property, you get an object with one property. If you just want the property value(s), use the -ExpandProperty parameter.

Upvotes: 3

Related Questions