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
4
  • 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] Commented Nov 24, 2016 at 21:48
  • sorry, forgot to add i have already done that, will edit the orginial post to show what i have. Commented Nov 24, 2016 at 22:01
  • Possible duplicate of Create variable from CSV Commented Nov 25, 2016 at 6:38
  • I tried to make a canonical "No to variable variables" "no to dynamic variable names in powershell" post here: stackoverflow.com/a/40477933/478656 even though that question was about CSVs, your question is fundamentally the same and I think my answer applies. Commented Nov 25, 2016 at 6:38

1 Answer 1

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 ''
Sign up to request clarification or add additional context in comments.

7 Comments

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
"tempArray$i" = New-Object PSCustomObject Still trying to work out how this language works...thx
New-Variable -Name "tempArray$i" -Value $(NewObject psobject). What are you trying to accomplish exactly? Variable variable names is usually a code smell
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 I get the sentiment, but in terms of reducing unnecessary code, I think the variable variable names approach is misguided
|

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.