Home > Software engineering >  match one of two pid's using the processes full path
match one of two pid's using the processes full path

Time:12-12

I have two identical running processes called RocketLeague.exe

I want to store one of the processes PID's in a variable for later use in another command by matching both processes full file paths.

I have been able to come up with two commands so far that pipe the full path of the processes but can't figure out how to continue the piping of the right PID into a final custom command.

How can I store the correct PID in a variable for use in my custom command?

1) Get-Process -Name 'RocketLeague' | Format-List -Property Path
2) Get-Process -Name 'RocketLeague'

CodePudding user response:

Using the feedback from user:lit I was able to come up with this solution.

$procID = Get-process -Name 'RocketLeague' | Select-Object -Property Id,Path | ForEach-Object {
    If($_.Path -eq 'C:\Program Files (x86)\Steam\steamapps\common\rocketleague\Binaries\Win64\RocketLeague.exe'){
        Set-Variable -Name 'procSteam' -Value $_.Id; Write-Host $procSteam
        }
    }

CodePudding user response:

If you just want the specific Process that is equal to that Path you can use Where-Object or .Where() method for filtering. The code would be reduced to:

# This:
$procID = (Get-Process -Name 'RocketLeague').Where({
    $_.Path -eq 'C:\Program Files (x86)\Steam\steamapps\common\rocketleague\Binaries\Win64\RocketLeague.exe'
}) | Select-Object -Property Id, Path

# Or this:
$procID = Get-Process -Name 'RocketLeague' | Where-Object {
    $_.Path -eq 'C:\Program Files (x86)\Steam\steamapps\common\rocketleague\Binaries\Win64\RocketLeague.exe'
} | Select-Object -Property Id, Path

And if, for example there is only one of the paths that ends with Win64\....exe you can use .EndsWith() method:

$procID = (Get-Process -Name 'RocketLeague').Where({
    ([string]$_.Path).EndsWith('\Win64\RocketLeague.exe')
}) | Select-Object -Property Id, Path
  • Related