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
- Copy the script below and save it as Start-ProcessMonitor.ps1
- Run it with the following parameters -ProcessName and -FilePath
- 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
- 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'
- 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.
Save this as Start-ProcessMonitor.ps1
#requires -Version 2
Monitor a process, and keep it running!
We want to monitor a specefic process, and then be able to start it, if it is not running.
Enter the name of the process to monitor, with out the .exe extention
Path to the ProcessName file to monitor.
Start-ProcessMonitor.ps1 -ProcessName CMD -FilePath "C:\Windows\System32"
Will Launch and monitor the command prompt CMD
File Name: Start-ProcessMonitor.ps1
Author: Christian Tukjær Nyhuus - email@example.com
Requires: PowerShell V2
[Parameter(Mandatory = $true, Position = 0)]
$ProcessName = '',
[Parameter(Mandatory = $true, Position = 1)]
$FilePath = ''
# Running a neverending loop, because $true will always be $true
If(!(Get-Process -Name $ProcessName -ErrorAction SilentlyContinue))
# Setting timestamp for the log message
$TimeStamp = (Get-Date).ToString("yyyy/MM/dd HH:mm:ss")
# Generating the path for the log file, using the currently invoked location and file name, and add .log to the end of it.
$LogPath = "$($PSScriptRoot)\$($MyInvocation.MyCommand.Name)-$($ProcessName).log"
# Generating the message to add to the log file
$LogMessage = "$($TimeStamp): NOT RUNNING: $($ProcessName)... Launching it..."
# Adding the message to the log file.
Add-Content -Path $LogPath -Value $LogMessage
# Starting Process
Start-Process -FilePath "$($FilePath)\$($ProcessName).exe"
# Sleeping the loop for 5 secs, to let the process start.
Start-Sleep -Seconds 5
# Sleeping the loop for 5 secs, to not max out the CPU
Start-Sleep -Seconds 5