title: Detect number of processes running with the same name
body: <p>Any ideas of how to write a function that returns the number of instances of a process is running?</p>
  
  <p>Perhaps something like this?</p>
  
  <pre><code>function numInstances([string]$process)
  {
      $i = 0
      while(&lt;we can get a new process with name $process&gt;)
      {
          $i++
      }
  
      return $i
  }
  </code></pre>
  
  <p>Edit: Started to write a function... It works for a single instance but it goes into an infinite loop if more than one instance is running:</p>
  
  <pre><code>function numInstances([string]$process)
  {
  $i = 0
  $ids = @()
  while(((get-process $process) | where {$ids -notcontains $_.ID}) -ne $null)
      {
      $ids += (get-process $process).ID
      $i++
      }
  
  return $i
  }
  </code></pre>