title: How can I get PID to kill a process using PowerShell
body: <p>I'd like to kill all process that are not in a text file <code>$fileServices</code>,
  I use a loop to got PID to send it to <code>Stop-Process</code>.
  Runs without error. But the process isn't killed and my loop is not good.</p>
  
  <p>How can I got PID to kill this process using PowerShell please?</p>
  
  <pre><code>$ServiceName = Get-WmiObject Win32_Process -Filter "name = 'service.exe'" |
                 Select-Object CommandLine
  $procId = Get-WmiObject Win32_Process -Filter "name = 'service.exe'" |
            Select-Object ProcessId
  $fileServices = Get-Content C:\serviceNotStop.txt
  
  $totalcount = Get-Content C:\serviceNotStop.txt | Measure-Object -Line
  $number = $totalcount.Lines
  
  $get_path = $ServiceRunName.CommandLine
  $output_file = 'C:\service_name.txt'
  $get_path | Out-File -FilePath $output_file
  
  if ($fileServices -ne $null) {
      foreach ($process in $get_path ) {
          foreach ($id in $procId ) {
              $nameExtracted = ($process -split '//')[2]
              $counter = 1
              foreach ($line in $fileServices) {
                  if ($line -ne $nameExtracted) {
                      $counter++
                      "continue search file, $counter"
                      if ($counter -gt $number) {
                          " kill process after all search"
                      } else {
                          continue
                      }
                  } elseif ($line -eq $nameExtracted) {
                      "keep process $line "
                      break
                  }
              }
          }
      }
  } else {
      "the file is empty"
  }
  </code></pre>