Noob help please. I\'m trying to write a script that will check if a process is running, and if not, start it. If the process is running, it should do nothing. I\'ve come up wit
$path = "C:\utilities\prog.exe"
$list = get-process | where-object {$_.Path -eq $path }
if ($list -eq $null) { start-process -filepath $path }
Or
$path = "C:\utilities\prog.exe"
get-process | where-object {$_.Path -eq $path } | measure-object | where-object { $_.Count -eq 0} | foreach-object {start-process -filepath $path }
This will save you the if statement and one variable declaration. It can be a one line command as well:
Don't let the foreach at the end put you off. The measure object will return only one item the where will return one or zero. The foreach is only used to execute if the result is zero. It won't start more then one processes. :)