Reading cmdlet output

suggest change

By default, PowerShell returns output to the caller. For example:

Get-Process -Name excel

This returns each running process named excel to the caller. In this case, that is the PowerShell host. It prints something like:

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id  SI ProcessName                                                                                                                     
-------  ------    -----      ----- -----   ------     --  -- -----------                                                                                                                     
   1037      54    67632      62544   617     5.23   4544   1 EXCEL

If you assign the output to a variable, it is not printed. The variable holds the output, whatever its type.

$allExcel = Get-Process -Name excel

To also store the output under a variable name while still writing it to the pipeline, use -OutVariable:

Get-Process -Name excel -OutVariable AllRunningExcel

The name is written without $. Unlike a normal assignment, -OutVariable still writes the objects to the pipeline. You can also capture that pipeline output in another variable:

$VarOne = Get-Process -Name excel -OutVariable VarTwo

This is uncommon. Both $VarOne and $VarTwo hold the same objects.

Now consider this,

Get-Process -Name EXCEL -OutVariable MSOFFICE
Get-Process -Name WINWORD -OutVariable +MSOFFICE

The first statement gets the Excel process and assigns it to $MSOFFICE. The + before the second name appends the Word processes to that variable. The host still prints both results:

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id  SI ProcessName                                                                                                                     
-------  ------    -----      ----- -----   ------     --  -- -----------                                                                                                                     
   1047      54    67720      64448   618     5.70   4544   1 EXCEL                                                                                                                           
   1172      70    50052      81780   584     1.83  14968   1 WINWORD

Feedback about page:

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



Table Of Contents