Multiple Assignments in One Line

by Jan 31, 2014

When you assign something to a variable, you can enclose the expression in braces. This will also output the data. Have a look:

$a = Get-Service
($a = Get-Service) 

See the difference? The second line will not only assign the results from Get-Service to the variable, but also output them to the console.

Actually, you can use the second set of results yourself, too. Have a look at this line:

$b = ($a = Get-Service).Name

This will assign all services to $a, and all service names to $b.

Again, you can enclose this in braces to further reuse the results:

$c = ($b = ($a = Get-Service).Name).ToUpper()

Now $c will also contain the service names in all uppercase letters. Pretty freaky stuff.

Twitter This Tip! ReTweet this Tip!