title: Assure only 1 instance of PowerShell Script is Running at any given Time
body: <p>I am writing a batch script in PowerShell v1 that will get scheduled to run let's say once every minute. Inevitably, there will come a time when the job needs more than 1 minute to complete and now we have two instances of the script running, and then possibly 3, etc...</p>
  
  <p>I want to avoid this by having the script itself check if there is an instance of itself already running and if so, the script exits.</p>
  
  <p>I've done this in other languages on Linux but never done this on Windows with PowerShell.</p>
  
  <p>For example in PHP I can do something like:</p>
  
  <pre><code>exec("ps auxwww|grep mybatchscript.php|grep -v grep", $output);
  if($output){exit;}
  </code></pre>
  
  <p>Is there anything like this in PowerShell v1? I haven't come across anything like this yet.</p>
  
  <p>Out of these common patterns, which one makes the most sense with a PowerShell script running frequently?</p>
  
  <ol>
  <li>Lock File</li>
  <li>OS Task Scheduler</li>
  <li>Infinite loop with a sleep interval</li>
  </ol>