I have a procedure in which the number of variables I need may vary. I thought PowerShell had this functionality.
Just want to share.
clear the screen
declare 2 variable areas and populate with numbers 1 to 1000
Then replace numbers in the Second Array.
Note: First Array variable is 0: Â $b[$_ – 1]
So I have to substract 1
cls
$a=@(1..1000)
$b=@(1..1000)
$a | %{ $b[$_-1]=”var“+$_}
$b
Output:
var1
var2
.
.
.
var998
var1000
If you have any examples where you need sequenced variables chime in and share too.
A better way:
A more complete procedure:
for ($i=0; $i –le 1000; $i++)
{
New-Variable -Name “var$i” -value $i
Get-Variable -Name var$i –ValueOnly
}
Found http://stackoverflow.com/questions/13015303/dynamically-create-variables-in-powershell
Actually I’ve got a little farther to go to get variables, I’ve got an array of sequenced names.