1

Realizing it probably it may not be best practices, is there any other reason to not reuse the current item variable in a foreach as such:

foreach ($a in $something)
{Write-host $a}
.
.
.
foreach ($a in $somethingelse)
{Write-host $a}

In other words, is it always going to be best to in that second case use a different variable ($b?)

4 Answers 4

4

As briantist mentions this is fine. However when I do a foreach loop I tend to keep it specified to whatever you're taking it from. I find this will make it easier to comprehend further down the line.

#Example 1
foreach ($item in $array)
{Write-Host $item}

#Example 2
foreach ($user in $userList)
{Write-Host $user}

#Example 3
foreach ($breed in $dogBreed)
{Write-Host $breed}
Sign up to request clarification or add additional context in comments.

2 Comments

I agree with this. If it makes sense for the subsequent foreach loops to use same instance variable, then I will reuse it as the poster has done, and if it doesn't make sense I will use a new name. Essentially, aim for readability.
Thanks for the clarification. In my case I'm pulling from 2 different lists of computers, so foreach ($computer in $list1) and foreach ($computer in $list2) still makes sense as to what is being used.
2

There's nothing wrong with that. The variable $a will always be set to the current item inside the foreach loop's body.

Comments

1

One reason why this isn't a idea is that variable $a retains it's value (within the assigned scope) until it is reassigned. That may not sound like a big issue, but occasionally PowerShell does something unexpected and it just isn't worth the time troubleshooting to figure something like that out. Consider this function:

function reuse-vars {

$arrayA = 0..3
$arrayB = 10..13
$arrayC = 20..23

    foreach ( $a in $arrayA ){
        Write-Host "`$a is $a"
        Write-Host "`$b is $b"
        Write-Host "`$c is $c"
    }

    foreach ( $b in $arrayB ){
        Write-Host "`$a is $a"
        Write-Host "`$b is $b"
        Write-Host "`$c is $c"
    }

    foreach ( $c in $arrayC ){
        Write-Host "`$a is $a"
        Write-Host "`$b is $b"
        Write-Host "`$c is $c"
    }

}

$a will retain the value from the last foreach iteration until it is reassigned, that includes within nested foreach or if they are in series.

C:\> reuse-vars
$a is 0
$b is
$c is
$a is 1
$b is
$c is
$a is 2
$b is
$c is
$a is 3
$b is
$c is
$a is 3
$b is 10
$c is
$a is 3
$b is 11
$c is
$a is 3
$b is 12
$c is
$a is 3
$b is 13
$c is
$a is 3
$b is 13
$c is 20
$a is 3
$b is 13
$c is 21
$a is 3
$b is 13
$c is 22
$a is 3
$b is 13
$c is 23

It may not matter depending on what you're doing but the possibility for this to cause an issue makes it worthwhile to dream up a different variable name for each foreach (like $b).

1 Comment

I don't think this is any different than any other variable retaining its value though. This also isn't really addressing the scenario asked about in the question.
0

I agree with the others that this should work just fine, but that using different names is better practice.

However, if you're paranoid you can always clear or delete the variable manually between the loops:

foreach ($a in $something)
{Write-host $a}
.
$a = $null;
.
foreach ($a in $somethingelse)
{Write-host $a}

Or:

foreach ($a in $something)
{Write-host $a}
.
Remove-Variable -Name a;
.
foreach ($a in $somethingelse)
{Write-host $a}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.