Outputting and Assigning at the Same Time

by Sep 7, 2015

If you’d like to assign the results of a command to a variable, and at the same time output the results, too, here are two approaches.

You can either use parenthesis:

 
PS> ($result = Get-Service)

Status   Name               DisplayName                           
------   ----               -----------                           
Running  AdobeARMservice    Adobe Acrobat Update Service      
(...)
 

Or, you can use the –OutVariable common parameter:

 
PS> Get-Service -OutVariable result

Status   Name               DisplayName                           
------   ----               -----------                           
Running  AdobeARMservice    Adobe Acrobat Update Service  
(...)
 

Twitter This Tip! ReTweet this Tip!