Get-Process from path

Hi Comunity,

One of our developers uses Task Scheduler to run his programs. The problem comes when the process fails, the task has no idea about this and the task will not be re-triggered. This is the point where a script comes in that checks that the process is running, and if not the task is triggered to start the process. I have come so far:

$taskPath = "\"

Get-ScheduledTask -TaskPath $taskPath |
ForEach-Object { if ($_.actions.execute -match ".exe") {
if ()

the link missing is how do i now connect the task with the process so i can check the state…

Get-Process can’t query processes by their filename. However, the WMI/CIM Win32_Process class can be filtered by path.

Thank you for the answer, Don. Stupid question following: how can i filter a CimInstance. I have tried with path and did not work,

Get-CimInstance Win32_Process -Filter "Path = 'C:\Windows\system32\notepad.exe'"

But this works:

Get-CimInstance Win32_Process -Filter "name = 'notepad.exe'"

What am i missing?

Is this what you are after?

(Get-Process -Name notepad).Path
C:\WINDOWS\system32\notepad.exe

Yet, what you are doing should work, as shown below, with another example of how you could approach it.

Get-CimInstance Win32_Process -Filter "name = 'notepad.exe'"

ProcessId Name        HandleCount WorkingSetSize VirtualSize  
--------- ----        ----------- -------------- -----------  
1320      notepad.exe 239         18063360       2203497508864



(Get-CimInstance Win32_Process) -match 'notepad'

ProcessId Name        HandleCount WorkingSetSize VirtualSize  
--------- ----        ----------- -------------- -----------  
1320      notepad.exe 239         18063360       2203497508864

Filtering Path within the wmi command seems to be a special case. Maybe it’s created dynamically after the query?

https://social.technet.microsoft.com/Forums/en-US/f8a58bb4-81f5-4d67-8f2d-c958edb84f96/filter-win32groupuser-class-gt-invalid-query?forum=winserverpowershell

Try “commandline” or “executablepath” instead. Like in sql, you also have to double up the backslashes. Ah, the obstacle course of scripting.

Get-Ciminstance Win32_Process -Filter "ExecutablePath = 'C:\\Windows\\system32\\notepad.exe'"

ProcessId Name        HandleCount WorkingSetSize VirtualSize
--------- ----        ----------- -------------- -----------
6748      notepad.exe 230         14123008       2199194226688

Thank you postanote, I was trying to match by Path.

The double backslashes were the missing link, that works, thank you js!

The problem is that I need a property from Get-ScheduledTask that i can then pipe to something that checks if the process is running, however if i need to use double backslashes, i cannot use the Path property to feed Get-CimInstance. I also do not have a Name that i can use. Any suggestions on how i can overcome this?

You can use replace. Have to double the backslashes once again for the first argument, since it’s a regex.

$path = 'c:\windows\system32\notepad.exe'
$path = $path -replace '\\','\\'
$path

c:\\windows\\system32\\notepad.exe

Hmm, wouldn’t this be easier?

get-process | where path -eq c:\windows\system32\notepad.exe

Uuu, much easier indeed. Thank you, js! I’ve got it running now.