title: powershell loop for periodical checking running process
body: <p>I'm trying to create a PowerShell script (loop) for periodical checking if some other PowerShell script with specific command is running. If not I start it.</p>
  
  <p>This is what I have:</p>
  
  <pre><code>$processInfo = Get-WmiObject Win32_Process -Filter "name = 'powershell.exe'" | select CommandLine | Out-String -width 200
  
  while($true) {
      if ($processInfo -NotLike '*specific_path*') {
          Write-Host 'process not running'
          Start-Process powershell -argument '-f X:\specific_path\other_script.ps1'
      }
      else { Write-Host 'process is running' }
      Start-Sleep -Seconds 60
  }
  </code></pre>
  
  <p>The script correctly detects running or not running process when started. but when situation changes during the script run, it does not detect it.
  Also, when script starts, detects that the other script is not running, it starts it correctly but then it does not see it already running and starts it again and again.</p>
  
  <p>So the only problem I have (I believe) is how to get "fresh" data about running processes. Any ideas? Many thanks!</p>