Check If a Program in a Specific Path Is Running

check if process is running with only Path provided

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.

Check a program existence in PATH

where checks, if the given file exists within the path (or on the current working folder %cd%) and gives either the full path(s) or an error message. Both you don't need - just the errorlevel:

where mingw32-make >nul 2>&1
if errorlevel 1 (echo "not exists") else (echo "exists")

or as a shortcut:

where /q mingw32-make && echo found || echo not found

Check if a program is running by executable path

Try this:

get-process | ?{$_.path -eq $path}

So you can do something like:

if(get-process | ?{$_.path -eq "C:\My Temporary Programs\Test 1.exe"}){
#exe is running. Do what you want
}

How to test if an executable exists in the %PATH% from a windows batch file?


for %%X in (myExecutable.exe) do (set FOUND=%%~$PATH:X)
if defined FOUND ...

If you need this for different extensions, just iterate over PATHEXT:

set FOUND=
for %%e in (%PATHEXT%) do (
for %%X in (myExecutable%%e) do (
if not defined FOUND (
set FOUND=%%~$PATH:X
)
)
)

Could be that where also exists already on legacy Windows versions, but I don't have access to one, so I cannot tell. On my machine the following also works:

where myExecutable

and returns with a non-zero exit code if it couldn't be found. In a batch you probably also want to redirect output to NUL, though.

Keep in mind

Parsing in batch (.bat) files and on the command line differs (because batch files have %0%9), so you have to double the % there. On the command line this isn't necessary, so for variables are just %X.

Check if path is in Program Files

You can check a path in ProgramFiles(x86) by using the code below:

string path = "yourpath";


var programfileX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);

if (path.IndexOf(programfileX86, StringComparison.OrdinalIgnoreCase) >= 0)
{
//Found path
}

Check if a specific exe file is running


bool isRunning = Process.GetProcessesByName("test")
.FirstOrDefault(p => p.MainModule.FileName.StartsWith(@"c:\loc1")) != default(Process);

Finding the path of the program that will execute from the command line in Windows

Use the where command. The first result in the list is the one that will execute.


C:\> where notepad
C:\Windows\System32\notepad.exe
C:\Windows\notepad.exe

According to this blog post, where.exe is included with Windows Server 2003 and later, so this should just work with Vista, Win 7, et al.

On Linux, the equivalent is the which command, e.g. which ssh.



Related Topics



Leave a reply



Submit