# Get-Process from path

**URL:** <https://forums.powershell.org/t/get-process-from-path/11407>\
**Category:** PowerShell Help\
**Created:** [October 3, 2018, 7:59am UTC](https://forums.powershell.org/t/get-process-from-path/11407 "2018-10-03T07:59:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbrezaie](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@cbrezaie](https://forums.powershell.org/u/cbrezaie)\
**Post date:** [October 3, 2018, 7:59am UTC](https://forums.powershell.org/t/get-process-from-path/11407/1 "2018-10-03T07:59:33Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [October 3, 2018, 9:19am UTC](https://forums.powershell.org/t/get-process-from-path/11407/2 "2018-10-03T09:19:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cbrezaie](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@cbrezaie](https://forums.powershell.org/u/cbrezaie)\
**Post date:** [October 3, 2018, 10:17am UTC](https://forums.powershell.org/t/get-process-from-path/11407/3 "2018-10-03T10:17:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [October 3, 2018, 10:48am UTC](https://forums.powershell.org/t/get-process-from-path/11407/4 "2018-10-03T10:48:14Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [October 3, 2018, 11:02am UTC](https://forums.powershell.org/t/get-process-from-path/11407/5 "2018-10-03T11:02:49Z")

</div>

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](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
```

---

<div class="post-metadata">

**Author:** ![cbrezaie](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@cbrezaie](https://forums.powershell.org/u/cbrezaie)\
**Post date:** [October 4, 2018, 2:06am UTC](https://forums.powershell.org/t/get-process-from-path/11407/6 "2018-10-04T02:06:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [October 4, 2018, 6:46am UTC](https://forums.powershell.org/t/get-process-from-path/11407/7 "2018-10-04T06:46:19Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![cbrezaie](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@cbrezaie](https://forums.powershell.org/u/cbrezaie)\
**Post date:** [October 4, 2018, 8:40am UTC](https://forums.powershell.org/t/get-process-from-path/11407/8 "2018-10-04T08:40:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/get-process-from-path/11407/9 "2024-05-16T20:34:17Z")

</div>

Closed on May 16, 2024, 8:34pm UTC


