In the previous tip we talked about logging script results, and how you can assign values and at the same time output the assigned values by using parentheses:
PS> ($a = Get-Process -Id $pid) Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 1595 102 283200 325444 64,56 6436 1 powershell_ise PS> $a Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 1595 102 283200 325444 64,75 6436 1 powershell_ise PS>
The same can be accomplished by using the –OutVariable common parameter:
PS> Get-Process -Id $pid -OutVariable b Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 1731 105 290336 341688 66,66 6436 1 powershell_ise PS> $b Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 1731 105 290336 341688 66,92 6436 1 powershell_ise PS>
And Tee-Object is a third approach:
PS> Get-Process -Id $pid | Tee-Object -Variable c Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 1759 109 292300 343644 71,53 6436 1 powershell_ise PS> $c Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 1759 109 292300 343644 71,69 6436 1 powershell_ise PS>
Since this is using the pipeline, it is much slower. When you avoid the pipeline, performance is better:
PS> Tee-Object -InputObject (Get-Process -Id $pid) -Variable d Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 1761 111 294568 345268 74,31 6436 1 powershell_ise PS> $d Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 1761 111 294568 345268 74,59 6436 1 powershell_ise PS>