Best Practice: It is recommended to avoid Out-Null when you need to suppress the output of some commands inside large loops (for, foreach, etc.) for example.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$array = New-Object -TypeName System.Collections.ArrayList # Out-Null (cmdlet) $array.Add('A') | Out-Null # $null (redirected to $null) $array.Add('B') > $null # [void] (cast to void) - RECOMMENDED [void]$array.Add('C') # $null (assigned to $null) - RECOMMENDED $null = $array.Add('D') |
Explanation:
It is recommended to assign to $null or [void] casting for better performance.
1 2 |
[void]$array.Add('C') $null = $array.Add('D') |
When you want to suppress the output of a command (example: when adding items to a collection), using Out-Null is slow.
Results of a test with a large loop:
Pingback: Powershell Best Practice #13: Avoid double quotes in strings if not necessary | Powershell Guru