List Assignment of Multiple Variables

suggest change

Powershell allows multiple assignment of variables and treats almost everything like an array or list. This means that instead of doing something like this:

$input = "foo.bar.baz"
$parts = $input.Split(".")
$foo = $parts[0]
$bar = $parts[1]
$baz = $parts[2]

You can simply do this:

$foo, $bar, $baz = $input.Split(".")

Since Powershell treats assignments in this manner like lists, if there are more values in the list than items in your list of variables to assign them to, the last variable becomes an array of the remaining values. This means you can also do things like this:

$foo, $leftover = $input.Split(".") #Sets $foo = "foo", $leftover = ["bar","baz"]
$bar = $leftover[0] # $bar = "bar"
$baz = $leftover[1] # $baz = "baz"

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents