How Do I Access Variables with Embedded Names in PowerShell?

0
0
Asked By ScriptSavvy101 On

Hey everyone! I'm just starting out with PowerShell and trying to write some scripts to help me learn. I've hit a bit of a snag when it comes to accessing variables that have other variables embedded in their names. I'm working with a couple of arrays and I want to cycle through them, using a loop to display the values with `Write-Host`. I tried embedding `$i` into the variable name in the `Write-Host` line, but it didn't work the way I thought it would. Can anyone guide me on how to properly do this? Here's the code I'm working with:

```powershell
$totalArrays = 3
$myArray0 = @("red", "yellow", "blue")
$myArray1 = @("orange", "green", "purple")
$myArray2 = @("black", "white")

for ($i = 0; $i -lt $totalArrays; $i++) {
Write-Host $myArray$i
}
```

5 Answers

Answered By CodingNinja92 On

You're trying to dynamically access the variable, which is doable using `Get-Variable`, but it's not the safest approach. Instead, consider using an array or a jagged array to store your arrays. Here's a smoother way to do it:

```powershell
$arrays = @($myArray0, $myArray1, $myArray2)
for($i = 0; $i -lt $arrays.count; $i++) {
Write-Host $arrays[$i]
}
```
Or you can use a `foreach` loop:

```powershell
foreach($singleArray in $arrays) {
Write-Host $singleArray
}
```

Answered By LearningPowerShell On

I appreciate the insights, @CodingNinja92 and @PowerShellWiz! I'm really interested in how PowerShell processes these commands. For my project, I'm definitely planning to use a jagged array instead of trying to create 'dynamic' variable names like you guys suggested. Thanks again for the help!

TechGuruX -

If you're curious about how PowerShell parses commands, check out the `ScriptBlock.Ast` property. You can inspect it like this:

```powershell
$Script = {
$var1 = @(1, 2, 3)
$var2 = @(4, 5, 6)
$one = 1
Write-Host $var$one
}
$Script.Ast.EndBlock.Statements[-1] `
.PipelineElements[0] `
.CommandElements
```
In this case, `$var$one` is being interpreted as a string, so make sure you understand how that works!

Answered By ArrayMaster On

A great way to manage this is with hashtables. They link a key to a value, which can be your arrays. Check this out:

```powershell
$colourArrays = @{}
$colourArrays[0] = @("red", "yellow", "blue")
$colourArrays[1] = @("orange", "green", "purple")
$colourArrays[2] = @("black", "white")

for ($i = 0; $i -lt $colourArrays.count; $i++) {
Write-Host $colourArrays[$i]
}
```
This way, you're not tied to just numbers and can use different keys like strings for better organization.

Answered By PowerShellWiz On

Keep in mind, variable indirection is also possible in PowerShell, though I'd suggest you avoid it because it complicates your code. You can do something like this:
```powershell
$var = 'tree'
$tree = 'ash'
```
Then using `$$var` would give you 'ash'. But really, it's best to keep things straightforward to avoid confusion or flagging issues!

Answered By TechGuruX On

Just to clarify, `$$var` might not work in all PowerShell versions, particularly in 5.1 or 7.5.1. Just be aware of the nuances!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.