Set multiple variables at once in PowerShell
A list of variable names can be an l-value, that is, an expression that appears on the left side of an assignment operator. Did you know that you can do this…
PS C:\> $FirstName, $Initial, $LastName = 'John', 'Q', 'Public'
This statement will assign to each variable the corresponding value on the right of the assignment operator. What if the number of variable names and the number of values doesn’t match? Extra values will be stored as an array in the last variable.
PS C:\> $FirstName, $Initial, $LastName = 'John', 'Q', 'Public', 'Sr', 'Esq'
That $LastName
variable will be a list containing Public, Sr, and Esq. If there aren’t enough values to go around, the extra variables are unassigned.
Oh, and if you want to set several variable names to the same value, use this:
PS C:\> $a = $b = $c = $d = 90
Have fun with your variables.
Be seeing you.
One thought on “Let x = a whole bunch of things”