title: Pass / Pipe / Loop All processes from Get-Process to Powershell PoshInternals script
body: <p>How would I pass all processes with Get-Process matching a certain process name  to another PowerShell script one by one ?</p>
  
  <p>pseudocode</p>
  
  <pre><code>for each process in matchingprocesses:
    myScript(process)
  </code></pre>
  
  <p>Planned usage Set-WorkingSetToMin script: <a href="https://www.powershellgallery.com/packages/PoshInternals/1.0/Content/Set-WorkingSetToMin.ps1" rel="nofollow noreferrer">https://www.powershellgallery.com/packages/PoshInternals/1.0/Content/Set-WorkingSetToMin.ps1</a></p>
  
  <p>This works great as there is only one notepad++ process:</p>
  
  <pre><code>get-process notepad++ | Set-WorkingSetToMin 
  </code></pre>
  
  <p>However for VS Code this only only gets the first code process and ignores the rest:</p>
  
  <pre><code>get-process code | Set-WorkingSetToMin
  </code></pre>
  
  <p>How do I pipe each process matching a certain name to powershell script ?</p>
  
  <p>Alternative would be to modify PoshInternals script to accept multiple processes:</p>
  
  <pre><code># Dont run Set-WorkingSet on sqlservr.exe, store.exe and similar processes
  # Todo: Check process name and filter
  # Example - get-process notepad | Set-WorkingSetToMin 
  Function Set-WorkingSetToMin {
      [CmdletBinding()]
      param(
      [Parameter(ValueFromPipeline=$True, Mandatory=$true)]
      [System.Diagnostics.Process] $Process
      )
  
  if ($Process -ne $Null)
  {
      $handle = $Process.Handle
      $from = ($process.WorkingSet/1MB) 
      $to = [PoshInternals.Kernel32]::SetProcessWorkingSetSize($handle,-1,-1) | Out-Null
      Write-Output "Trimming Working Set Values from: $from"
  
  } #End of If
  } # End of Function
  </code></pre>
  
  <p>ANSWER in one line without extra variables:</p>
  
  <pre><code>foreach ($process in Get-Process myprocessname) { Set-WorkingSetToMin ($process) }
  </code></pre>