# How to compare current and next Iteration values of foreach loop in Powershell

**URL:** <https://forums.powershell.org/t/how-to-compare-current-and-next-iteration-values-of-foreach-loop-in-powershell/10774>\
**Category:** PowerShell Help\
**Created:** [June 6, 2018, 12:11pm UTC](https://forums.powershell.org/t/how-to-compare-current-and-next-iteration-values-of-foreach-loop-in-powershell/10774 "2018-06-06T12:11:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dinesh1393](https://avatars.discourse-cdn.com/v4/letter/d/b487fb/32.png) [@dinesh1393](https://forums.powershell.org/u/dinesh1393)\
**Post date:** [June 6, 2018, 12:11pm UTC](https://forums.powershell.org/t/how-to-compare-current-and-next-iteration-values-of-foreach-loop-in-powershell/10774/1 "2018-06-06T12:11:56Z")

</div>

Hi,

I have a loop like below. In that, I want to compare next iteration value with current iteration value. If current value is not match with next value then I need to do some process. So I want to know like how to get the next Iteration value of a loop in powershell.

$var = (‘abc’,‘abc’,‘bcd’,‘cdf’,‘cdf’)  
Foreach($i in $var){  
If($i -ne $nextiterationvalue)  
{  
do something  
}  
else  
{  
do something  
}  
}

Thanks,  
Dinesh

---

<div class="post-metadata">

**Author:** ![jacorbello](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jacorbello](https://forums.powershell.org/u/jacorbello)\
**Post date:** [June 6, 2018, 12:27pm UTC](https://forums.powershell.org/t/how-to-compare-current-and-next-iteration-values-of-foreach-loop-in-powershell/10774/2 "2018-06-06T12:27:11Z")

</div>

In this case, you’d use a for loop to iterate over different entries of the array. See below:

```
for ($i = 0; $i -lt $var.count; $i++) {
    if ($var[$i] -ne $var[$i+1]) {
        do something
        }
    else {
        do something
        }
    }
```

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [June 6, 2018, 12:27pm UTC](https://forums.powershell.org/t/how-to-compare-current-and-next-iteration-values-of-foreach-loop-in-powershell/10774/3 "2018-06-06T12:27:40Z")

</div>

Within a ForEach, you can’t. But you could do a For loop.

```
$array = @("some","stuff")

for ($x = 0; $x -lt $array.count; $x++) {
  $array[$x] # current item
  $array[$x + 1] # next item
  $array.count # max items
}
```

Note that the “next item” will fail once you’re on the last item, so you’d need to add a check to not exceed $array.count.

---

<div class="post-metadata">

**Author:** ![dinesh1393](https://avatars.discourse-cdn.com/v4/letter/d/b487fb/32.png) [@dinesh1393](https://forums.powershell.org/u/dinesh1393)\
**Post date:** [June 8, 2018, 2:36am UTC](https://forums.powershell.org/t/how-to-compare-current-and-next-iteration-values-of-foreach-loop-in-powershell/10774/4 "2018-06-08T02:36:53Z")

</div>

Thanks! It worked very well.

---

<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:34pm UTC](https://forums.powershell.org/t/how-to-compare-current-and-next-iteration-values-of-foreach-loop-in-powershell/10774/5 "2024-05-16T20:34:56Z")

</div>

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


