PowerShell: How to automatically restart applications when they were closed

If you want an application always started and available then this article is for you. Sometimes an application is mistakenly closed by the user. This article shows a step-by-step scenario how to configure a scheduled task watching the status of your application.


Well, we are going to use a PowerShell three-iner. For the execution of PowerShell script files we have to take a glance at the PowerShell Executionpolicy. Make sure the policy setting is configured to remotesigned (recommended) or unrestricted.



If you see Restricted, run

Set-ExecutionPolicy remotesigned -Force


PowerShell script for checking the status of the application (started / not started)

Open PowerShell ISE. (Type ise in PowerShell). Copy the code into your ISE session.

If (!(Get-Process -Name notepad.exe -ErrorAction SilentlyContinue))
{Invoke-Item C:\Windows\notepad.exe

Let’s stay here for a short explanation. The If Statement checks if notepad.exe is running. The ! at the beginning of the command is a NOT statement. Only if notepad is not running, Invoke-Item will start notepad again.

Save the script. I name it notepad.ps1.


Now we can move on and put the script in a scheduled task. Unfortunately, I have to tell you this is not a good idea. Why? PowerShell will pop up every time the script runs. That’s annoying. The good news: I have a workaround for that. We call the PowerShell script from a VB script!

Creating the VB script

Open notepad. Type the following:

command = "powershell.exe -nologo -command C:\Temp\notepad.ps1"
set shell = CreateObject("WScript.Shell")
shell.Run command,0

Unbenannt.PNGNow we have two files: A PowerShell Script and a VB Script.


Configuring a Scheduled Task to run every 60 seconds

Open PowerShell ISE. First, think about which user you want to create the task for. In the following code example the task will be configured for the local user account patrick. Pay also attention to the path of your VB script (Line 1):

$Action=New-ScheduledTaskAction -Execute "C:\Temp\notepad.vbs"
$Trigger=New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 1)
$Principal=New-ScheduledTaskPrincipal -UserId "patrick" -LogonType Interactive
$Task=New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Set -Principal $Principal
Register-ScheduledTask -TaskName "Start Notepad" -InputObject $Task

When Patrick logs on and if notepad is not started, then the task will start it. That happens every 60 seconds. Configurations less than 1 minute are not supported.

No pain no gain

End of subject.

Writing that Scheduled Task script was a nightmare. I had a typo and a logical error and couldn’t find them for over an hour. No pain no gain. 😉

Don’t worry. All of the above is working. Have fun with it!

Further thoughts

If you like Scheduled Tasks and PowerShell, then have a look at my articles

How to schedule software installation with PowerShell

Configuring the automatic start of PowerShell at every logon

2 replies »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.