# Checking if a script is running

**URL:** <https://forums.powershell.org/t/checking-if-a-script-is-running/12747>\
**Category:** PowerShell Help\
**Created:** [June 19, 2019, 7:32am UTC](https://forums.powershell.org/t/checking-if-a-script-is-running/12747 "2019-06-19T07:32:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nathan-whitehouse-2](https://avatars.discourse-cdn.com/v4/letter/n/f04885/32.png) [@nathan-whitehouse-2](https://forums.powershell.org/u/nathan-whitehouse-2)\
**Post date:** [June 19, 2019, 7:32am UTC](https://forums.powershell.org/t/checking-if-a-script-is-running/12747/1 "2019-06-19T07:32:54Z")

</div>

Hello all.

I have written a file watcher script that loops from the time it starts until about 11:00 PM.

It is an important script so I want a way to make sure it is running.  
It is launched via the windows task scheduler using the “Powershell -File c:\Scripts\Script.ps1”

I can see that the script is running as the logs it produces continue to increment.  
In my Health Monitor script I am using the following command to try and determinant if the script is running.

Get-WmiObject Win32\_Process -Filter “Name=‘powershell.exe’ AND CommandLine LIKE ‘%Script.ps1%’”

Unfortunately it is not returning any info.

Any help is greatly appreciated!

&nbsp;

---

<div class="post-metadata">

**Author:** ![fredrik-kacsmarck](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@fredrik-kacsmarck](https://forums.powershell.org/u/fredrik-kacsmarck)\
**Post date:** [June 19, 2019, 9:23am UTC](https://forums.powershell.org/t/checking-if-a-script-is-running/12747/2 "2019-06-19T09:23:18Z")

</div>

Are the logs produced by the script or by the scheduled task?  
Meaning if the script produce the log entries then it’s running so I doubt that you’ll catch an issue with FileWatcher that way.

Anyway filters in general can be a bit quirky.  
So I would suggest to do it in “two” steps.

E.g.  
[pre]  
$process = Get-WmiObject Win32\_Process -Filter “Name=‘powershell.exe’” | Where-Object {$\_.CommandLine -like “_script.ps1_”}  
[/pre]

---

<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:** [June 19, 2019, 1:20pm UTC](https://forums.powershell.org/t/checking-if-a-script-is-running/12747/3 "2019-06-19T13:20:28Z")

</div>

Do this as a PowerShellJob and just monitor the job.

---

<div class="post-metadata">

**Author:** ![nathan-whitehouse-2](https://avatars.discourse-cdn.com/v4/letter/n/f04885/32.png) [@nathan-whitehouse-2](https://forums.powershell.org/u/nathan-whitehouse-2)\
**Post date:** [June 19, 2019, 2:39pm UTC](https://forums.powershell.org/t/checking-if-a-script-is-running/12747/4 "2019-06-19T14:39:31Z")

</div>

The script is doing the logging. Not the task scheduler.

I did try your command and it still returns no results.

&nbsp;

Thanks!

---

<div class="post-metadata">

**Author:** ![fredrik-kacsmarck](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@fredrik-kacsmarck](https://forums.powershell.org/u/fredrik-kacsmarck)\
**Post date:** [June 20, 2019, 5:43am UTC](https://forums.powershell.org/t/checking-if-a-script-is-running/12747/5 "2019-06-20T05:43:25Z")

</div>

I would recommend you the same answer as postanote.

The commandline I sent you will work if the powershell process that you want to check against is started via commandline.  
E.g. from normal cmd.exe.

[pre]  
c:&gt;powershell -file _path\_to\_your\_script_.ps1  
[/pre]

Then checking from a second script if it’s still running.  
If the first script is started from within a running powershell session then it won’t show up in CommandLine.  
Since that was not the command line starting the process.

But again, look into Start-Job and Receive-Job since that will be a better solution.

---

<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:32pm UTC](https://forums.powershell.org/t/checking-if-a-script-is-running/12747/6 "2024-05-16T20:32:07Z")

</div>

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


