Don’t do that: The following code requires multiple lines to read variables.
1 2 3 4 5 6 7 8 9 10 |
$variable1 $variable2 $variable3 $variable4 $variable5 $variable6 $variable7 $variable8 $variable9 $variable10 |
Do that: It’s possible to read multiple variables in one line and display the name/value:
1..10 | ForEach-Object -Process {Get-Variable -Name number$_}
(or using a for loop)
1 2 3 4 5 6 7 8 9 10 |
# Alternative 1 for ($i = 1; $i -le 10; $i++) { Get-Variable -Name $("number$i") } # Alternative 2 for ($i = 1; $i -le 10; $i++) { Get-Variable -Name $("number$i") } |