Multi Variable Assignment in Powershell

An extremely powerful feature (that isn’t all that well known) is the ability to assign values to multiple variables in one line. Take the following example:

PS> $one,$two,$three = "first","second","third"
PS> $one;$two;$three

This technique can be used in custom functions to parse the $args value

PS> function show-greeting {$greeting,$name = $args; "Hello $name, $greeting"}
PS> show-greeting "Nice to meet you" "Andy"
Hello Andy, Nice to meet you

One last thing to note is what happens if you have more values than variables. Does it blow up. An error. Not really. Check this out

PS C:\Users\andys> $one,$two = "first","second","third",4
PS C:\Users\andys> $one
PS C:\Users\andys> $two
PS C:\Users\andys> $one.count
PS C:\Users\andys> $two.count
PS C:\Users\andys>

The last variable gets the remaining values assigned to it.


10 Responses to “Multi Variable Assignment in Powershell”

  1. Scripting Games 2008 Event 2 « Get-Powershell Says:

    […] check out my post on multi variable assignment to see how this […]

  2. PowerScripting Podcast Episode 21, "One-half of Jeffrey Snover" « PowerScripting Podcast Says:

    […] Assign multiple variables in one expression (Get-PowerShell blog). Andy gets all the air time today. […]

  3. Says:

    The Iphone 5 sports a 4-inch” retina” display, supports the high-speed 4 G LTE network?
    If the video below, which was my own fault. The new exterior fits comfortably in the palm of your hand!
    But Stat Counter, which measures traffic collected across a network of 3 million websites,
    calculates that Apple’s share of mobile devices in Singapore – iPad and Iphone – declined sharply last year.

  4. Blackberry Says:

    With the Korean firm’s cheaper phones also roaring ahead, it has announced a very good budget Android smartphone – the blackberry i5510. The usage of the gadget.

  5. Says:

    I take pleasure in, cause I found exactly what I used to be taking a look for.
    You have ended my four day lengthy hunt! God Bless you
    man. Have a great day. Bye

  6. Says:

    I am gebuinely halpy too glance at this blog posts hich
    carries plenty of useful information, thanks for providing such data.

  7. livres pdf Says:

    Right now it seems like Drupal is the best blogging platform available right now.
    (from what I’ve read) Is that what you’re using on your blog?

  8. Autobiographie d'un yogi pdf Says:

    Hi there to all, the contents existing at this web site are actually awesome for people experience, well,
    keep up the nice work fellows.

  9. Gene Says:

    It’s in point of fact a nice and helpful piece of information. I am happy
    that you simply shared this useful information with us.
    Please keep us informed like this. Thank you for sharing.

  10. Jed Pociengel Says:

    Hello, i believe that i noticed you visited my blog so i came to go back the favor?.I’m attempting to in finding issues to improve my site!I guess its ok to make use of some of your ideas!!|

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: