List Assignment of Multiple Variables
suggest changePowerShell can assign several variables from one list. Do not use $input for this; that name is an automatic variable. Instead of:
$text = "foo.bar.baz"
$parts = $text.Split(".")
$foo = $parts[0]
$bar = $parts[1]
$baz = $parts[2]
You can simply do this:
$foo, $bar, $baz = $text.Split(".")
If the list has more values than variables, the last variable becomes an array of the leftover values:
$foo, $leftover = $text.Split(".") # $foo = "foo"; $leftover = "bar","baz"
$bar = $leftover[0] # $bar = "bar"
$baz = $leftover[1] # $baz = "baz"
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents