# Powershell logic

**URL:** <https://forums.powershell.org/t/powershell-logic/21224>\
**Category:** PowerShell Help\
**Created:** [December 24, 2022, 9:26am UTC](https://forums.powershell.org/t/powershell-logic/21224 "2022-12-24T09:26:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bin](https://avatars.discourse-cdn.com/v4/letter/b/edb3f5/32.png) [@bin](https://forums.powershell.org/u/bin)\
**Post date:** [December 24, 2022, 9:26am UTC](https://forums.powershell.org/t/powershell-logic/21224/1 "2022-12-24T09:26:26Z")

</div>

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

```auto
$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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 24, 2022, 10:00am UTC](https://forums.powershell.org/t/powershell-logic/21224/2 "2022-12-24T10:00:04Z")

</div>

> [@bin](#):
>
> `$n=$proc.Count`

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. 😉

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. 👆🏽

---

<div class="post-metadata">

**Author:** ![bin](https://avatars.discourse-cdn.com/v4/letter/b/edb3f5/32.png) [@bin](https://forums.powershell.org/u/bin)\
**Post date:** [December 24, 2022, 1:51pm UTC](https://forums.powershell.org/t/powershell-logic/21224/3 "2022-12-24T13:51:15Z")

</div>

thank you very much, i will try again

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/powershell-logic/21224/4 "2024-05-16T20:16:11Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


