PowerShell: Check if a process is running and if not start it

Had a requirement to do this, and here’s what I came up with:

if((Get-Process -Name notepad -ErrorAction SilentlyContinue) -eq $null){

The thing that happens if the process we check for, could obviously be anything. You might want to send an email (use Send-MailMessage) or start a different executable or whatever.

Note that you don’t put .exe on the end of the executable name when using Get-Process.

This entry was posted in PowerShell and tagged , , , , , . Bookmark the permalink.

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.