Don’t do that #6: Use Clear-Host to prevent the output

By | February 24, 2015

Don’t do that: The following code clean the screen to prevent the output (the index of the item added into the collection) generated by the Add method.

$array = New-Object System.Collections.ArrayList
$array.Add('Item1')
Clear-Host

clear-host-output-powershell


Do that: In cases like this one, it’s better to use [void] or $null to prevent the output to be displayed.

This code returns an output with “0” (the index of the item added into the collection).

dont-do-that-6-array-0

This code returns no output.

$null = $array.Add('Item1')
or
[void]$array.Add('Item2')

dont-do-that-6-void-null-output

Note: There is also a cmdlet “Out-Null” but piping to Out-Null is slower.


previous-buttonnext-button

One thought on “Don’t do that #6: Use Clear-Host to prevent the output

  1. Pingback: Don't do that #5 : Specify the type of variable with Read-Host - Powershell Guru

Leave a Reply

Your email address will not be published. Required fields are marked *