How would I pass all processes with Get-Process matching a certain process name to another PowerShell script one by one ?
pseudocode
for each process in matchingprocesses:
myScript(process)
Planned usage Set-WorkingSetToMin script: https://www.powershellgallery.com/packages/PoshInternals/1.0/Content/Set-WorkingSetToMin.ps1
This works great as there is only one notepad++ process:
get-process notepad++ | Set-WorkingSetToMin
However for VS Code this only only gets the first code process and ignores the rest:
get-process code | Set-WorkingSetToMin
How do I pipe each process matching a certain name to powershell script ?
Alternative would be to modify PoshInternals script to accept multiple processes:
# 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
ANSWER in one line without extra variables:
foreach ($process in Get-Process myprocessname) { Set-WorkingSetToMin ($process) }
You could add a where clause to pull out just the processes you want, then pipe that set to Set-WorkingSetToMin. An example is below, but adjust as needed to pull exactly what you're looking for.
get-process | where {$_.ProcessName -like "*code*"} | Set-WorkingSetToMin
Update: I see what you're saying, the issue is not with the set of processes being sent, but how they're handled once they're there. To get around that, you could set a variable equal to the process set, then loop through them, calling the cmdlet each time. Something like this:
$processes = get-process | where {$_.ProcessName -like "code"} | Set-WorkingSetToMin
foreach ($process in $processes)
{
Set-WorkingSetToMin ($process)
}
ANSWER thanks to help from Landon: foreach ($process in Get-Process myprocessname) { Set-WorkingSetToMin ($process) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With