I wanted a process to be always running, so why not make a PowerShell Process Monitor for that?

The idea was to create a never ending script, to look for the process, and if it shutdown for what so ever reason, it should start it again.

The script is fairly simple. We use a While ($true) to make our never ending loop, in that we have a if() else() statement, to handle if the process is running or not.

 

How to use the PowerShell Process Monitor

Use this script on your own risk!
  1. Copy the script below and save it as Start-ProcessMonitor.ps1
  2. Run it with the following parameters -ProcessName  and -FilePath
  3. For my test i used TeamViewer
    Start-ProcessMonitor.ps1 -ProcessName TeamViewer -FilePath 'C:\Program Files (x86)\TeamViewer'
    -ProcessName  is the name of the EXE file to monitor, without the .EXE file extention, eg. TeamViewer (for TeamViewer.exe)
    -FilePath  is the path to where the EXE file is located, eg. C:\Program Files (x86)\TeamViewer
  4. If you see the error ‘Start-ProcessMonitor.ps1 cannot be loaded because running scripts is disabled on this system‘, you need to change your ExecutionPolicy.
    Check it with Get-ExecutionPolicy . In order to run my script it need to be RemoteSigned, Unrestricted or ByPass. RemoteSigned is recommended.
    TIP: You can run this in a Restricted situation, if you just use the -ExectuinPolicy RemoteSigned  as a parameter:
    PowerShell.exe -ExecutionPolicy RemoteSigned -File Start-ProcessMonitor.ps1 -ProcessName TeamViewer -FilePath 'C:\Program Files (x86)\TeamViewer'
  5. In the same path as the Start-ProcessMonitor.ps1, you will also find a log file, where a line is added for every time the script has started the process for you.

 

The script

Save this as Start-ProcessMonitor.ps1