1

I have this code:

$a1 = "value1"
$a2 = "value2"
$a3 = "value3"
$a4 = "value4"
$a5 = "value5"

DO {
    "Starting Loop $a1"
    $a1
    $a1++
    "Now `$a is $a1"
} Until ($a1 -eq "value5")

i try to make the loop stop once it reach value5. The question is how i can go pass through all the variables, so if $a1 is not value5 it go to $a2. Thanks.

Griz
  • 11
  • 2
  • @jsotola what should the title be? "Stop Do Until loop once it reach certain variable"? – Griz Dec 11 '20 at 02:39
  • okay, i using your title suggestion lol – Griz Dec 11 '20 at 02:48
  • Does this answer your question? [Dynamically create variables in PowerShell](https://stackoverflow.com/questions/13015303/dynamically-create-variables-in-powershell) – jsotola Dec 11 '20 at 02:52
  • 4
    @Griz - generally speaking, one otta not use a series of $Var names. instead, store them in a collection of some sort. that makes working thru them MUCH easier ... just use `foreach ($Item in $Collection)` ... [*grin*] – Lee_Dailey Dec 11 '20 at 02:56
  • @jsotola i don't want to create new variable everytime the loop run, I have a list of variables like the one on my question, i want the loop going to every one of the variable list and stop when the variable value is `value5`. btw sorry for my bad explaination, have no idea how to explain it, hope you get it. – Griz Dec 11 '20 at 02:59
  • 1
    @Griz, what Lee_Dailey is trying to say, is that this is an odd request. Most people do not use variables in this way. Most people would use something like an array. So you would have a single variable, which contains multiple items. And then you can loop though the array. Generally when odd questions like this come up, it's because you're likely trying to solve a problem with the wrong tools. – Chad Baldwin Dec 11 '20 at 06:39

1 Answers1

2

What you're trying ⚠️

You might get the variables using the Get-Variable cmdlet. By default the Get-Variable (and the counterpart Set-Variable) will include all known variables including other variables you created (e.g. $b1 = "valueB1"), automatic variables (e.g. $args) and everything that is inherited from a parent scope.
Therefore you need to be very careful with these cmdlets as you might easialy retrieve or overwrite the wrong variable.

$a1 = "value1"
$a2 = "value2"
$a3 = "value3"
$a4 = "value4"
$a5 = "value5"

$a = 1
DO {
    "Starting Loop `$a$a"
    Get-Variable -ValueOnly "a$a"
    Set-Variable "a$a" "NewValue$a"
    Get-Variable -ValueOnly "a$a"
    $a++
} Until ($a -gt 5)

But as already suggested, Don't use the -Variable cmdlets for dynamic variable names!.

Instead

Create a new custom list of variables using a hash table and read and write your values in there:

$a = @{}                        # create a hash table
1..5 |% { $a[$_] = "value$_" }  # count from 1 to 5 ($_ is the current item)
$a[3]                           # display the $a[3]

value3
iRon
  • 20,463
  • 10
  • 53
  • 79
  • Please [accept](https://stackoverflow.com/help/someone-answers) the answer (or tell me what is missing), so that I can use it for other duplicated questions as e.g.: [Use Dynamically Created Variable Name Within a Variable Name](https://stackoverflow.com/q/65331634/1701026) – iRon Dec 17 '20 at 08:59