smaxx1337
smaxx1337

Reputation: 136

check if process is running with only Path provided

I want to check in Powershell if a process is running with only providing the process's path.

As example, the path to the .exe is "C:\Users\Administrator\Desktop\1\hello.exe".

However, there are also multiple .exe's with the same name, just in a different path:

"C:\Users\Administrator\Desktop\1\hello.exe"
"C:\Users\Administrator\Desktop\2\hello.exe"
"C:\Users\Administrator\Desktop\3\hello.exe"

So I can not just do Get-Process "hello".

How can I find out if a process is running by providing a Path, instead of a process name? It also should return the PID of the specific process. Get-Process sadly does not even shows you the path to the process.

Upvotes: 0

Views: 990

Answers (1)

CherryDT
CherryDT

Reputation: 29012

You can filter by Path and select Id:

Get-Process | Where-Object {$_.Path -EQ "C:\Users\Administrator\Desktop\1\hello.exe"} | Select-Object Id

By the way:

Get-Process sadly does not even shows you the path to the process.

It does, if you ask it to (it returns an object like all those commands do, and it has a default selection of properties to list but that doesn't mean you can't access the remaining properties by specifically selecting them):

Get-Process explorer | Select-Object Path

(or use Select-Object * to see all available properties)

It appears you haven't yet understood how PowerShell objects work, so I'd recommend you to check out some tutorials about this topic, for instance this article about selecting and filtering and this one that goes more into detail about the filtering options.

Upvotes: 3

Related Questions