Reading a CmdLet Output

suggest change

By Default, powershell would return the output to the calling Entity. Consider Below Example,

Get-Process -Name excel

This would simply, return the running process which matches the name excel, to the calling entity. In this case, 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

Now if you assign the output to a variable, it simply wont print anything. And of course the variable holds the output. (Be it a string, Object - Any type for that matter)

$allExcel = Get-Process -Name excel

So, lets say you have a scenario where you want to assign a variable by a Dynamic name, you can use the -OutVariable parameter

Get-Process -Name excel -OutVariable AllRunningExcel

Note that the ‘$’ is missing here. A major difference between these two assignments is that, it also prints the output apart from assigning it to the variable AllRunningExcel. You can also choose to assign it to an another variable.

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

Albeit, the above scenario is very rare, both variables $VarOne & $VarTwo will have the same value.

Now consider this,

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

The first statement would simply get excel process & assign it to MSOFFICE variable, and next would get ms word processes running and “Append” it to the existing value of MSOFFICE. It would look something like this,

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