Reputation: 3073
How do I create a new variable each time a loop runs?
Something along the lines of
for ($i=1; $i -le 5; $i++)
{
$"var + $i" = $i
write-host $"var + $i
}
Upvotes: 38
Views: 101635
Reputation: 42035
Use New-Variable
and Get-Variable
(mind available options including scopes). E.g.
for ($i=1; $i -le 5; $i++)
{
New-Variable -Name "var$i" -Value $i
Get-Variable -Name "var$i" -ValueOnly
}
Upvotes: 59