0

I am new to powershell scripts and not sure how to achieve the below:

$finalArray = @()
$tempArray0 = 'A'
$tempArray1 = 'B'
$tempArray2 = 'C'
FOR (i=0; i -eq 5; i++) {$finalArray += $tempArray[i]}

$finalArray

Output Should be:

A
B
C
veggyaurus
  • 241
  • 1
  • 2
  • 11
  • Initialize the array first out of the loop then you can add more elements to it. $finalArray = New-Object System.Collections.Generic.List[System.Object] – Junaid Nov 24 '16 at 21:48
  • sorry, forgot to add i have already done that, will edit the orginial post to show what i have. – veggyaurus Nov 24 '16 at 22:01
  • Possible duplicate of [Create variable from CSV](http://stackoverflow.com/questions/40477171/create-variable-from-csv) – TessellatingHeckler Nov 25 '16 at 06:38
  • I tried to make a canonical "No to variable variables" "no to dynamic variable names in powershell" post here: http://stackoverflow.com/a/40477933/478656 even though that question was about CSVs, your question is fundamentally the same and I think my answer applies. – TessellatingHeckler Nov 25 '16 at 06:38

1 Answers1

4

If the variable name is itself variable, you'll have to use the Get-Variable cmdlet to retrieve its value:

$finalArray = @()
$tempArray0 = 'A'
$tempArray1 = 'B'
$tempArray2 = 'C'

for($i=0; $i -le 2; $i++) {
    $finalArray += (Get-Variable "temparray$i" -ValueOnly)
}

$finalArray

If you want to create variables with variable names, use the New-Variable cmdlet:

$Values = 'A','B','C'
for($i = 0; $i -lt $Values.Count; $i++){
    New-Variable -Name "tempvalue$i" -Value $Values[$i]
}

which would result in:

PS C:\> $tempvalue1
B

Although the above will solve the example you've presented, I can think of very few cases where you wouldn't be better of using a [hashtable] instead of variable variable names - they're usually an over-complication, and you'll end up with unnecessary code anyways because you need to calculate the variable names at least twice (during creation and again when reading the value).

From the comments, it sounds like you're trying to generate input for a password generator. This can be simplified grossly, without resorting to variable variable names:

# Create a hashtable and generate the characters 
$CharArrays = @{
    Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
    Numbers = 0..9
}

# Generate some letters for the  password
$PasswordChars = $CharArrays['Letters'] |Get-Random -Count 10

# Generate a few digits
$PasswordChars += $CharArrays['Numbers'] |Get-Random -Count 4

# Shuffle them around a bit
$PasswordChars = $PasswordChars |Sort-Object {Get-Random}

# Create your password
$Password = $PasswordChars -join ''
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • perfect thx, can i use the same Get-variable in a for loop to create the 3 tempArrays as well?? "$tempArray(Get-Variable $i -ValueOnly)" = New-Object PSCustomObject – veggyaurus Nov 24 '16 at 22:45
  • "tempArray$i" = New-Object PSCustomObject Still trying to work out how this language works...thx – veggyaurus Nov 25 '16 at 01:17
  • `New-Variable -Name "tempArray$i" -Value $(NewObject psobject)`. What are you trying to accomplish exactly? Variable variable names is usually a code smell – Mathias R. Jessen Nov 25 '16 at 01:17
  • have made an array from a value of A..Z 0..9 with a string value that i can then use to make a password for various devices. trying to remove unnecessary lines of code. dont want it random but – veggyaurus Nov 25 '16 at 01:46
  • 1
    @veggyaurus I get the sentiment, but in terms of reducing unnecessary code, I think the variable variable names approach is misguided – Mathias R. Jessen Nov 25 '16 at 02:09
  • myMy fail -- $newPasswordArray = [pscustomobject]@{a=1; b=2; c=3; d=4} – veggyaurus Nov 25 '16 at 02:10
  • I am actually trying to create my own lookup table... Given the Letter a..z or 1..9 it will give me my choice in alpha-numeric values. for example a=er, b=R5 etc etc. then given input it will create the given password.. little like a cipher table – veggyaurus Nov 25 '16 at 04:27