Powershell logic

hello
i have multiple process instance , let say with chrome.exe , and want to leave only one instance and stop all others , i have try with this

$proc = Get-process chrome
do {
$n=$proc.Count    
Stop-Process -name $(($chrome.Name)[$n])  # seem cannot use variable in index
$n--
}while ($n-gt 1)

what i ahve missed in my logic
thank you

The variable $proc is defined outside of the loop. That’s why $proc.Count always stays the same inside the loop. And since you set $n in each loop iteration it actually never changes its value. :wink:

Regardless of that … it might be just an example and the original process you like to treat is something else but even a single Chrome window is assembled from more than one process. If you kill these processes you will pretty likely kill the functionality of Chrome or at least damage it severely. :point_up_2:t4:

1 Like

thank you very much, i will try again