PowerShell Arrays and that annoying number on the screen
When you create an array and populate it in PowerShell the number of elements is written out to the screen, very annoying.
e.g.
function Array-Error()
{
$Array = New-Object System.Collections.ArrayList
$Return = "ReturnValue"
$Array.Add("Value1")
$Array.Add("Value2")
$Array.Add("Value3")
$Array.Add("Value4")
return $return
}
$display = Array-Error
write-host -ForegroundColor Red $display
Output
0 1 2 3 ReturnValue
So how do we stop the numbers from being displayed to the screen, while Fix 2 is the proper way I like fix 1 as it reminds me of passing things over to “/dev/null” when I was a UNIX chap.
function Array-Fix1()
{
$Array = New-Object System.Collections.ArrayList
$Return = "ReturnValue"
$Array.Add("Value1") > $null
$Array.Add("Value2") > $null
$Array.Add("Value3") > $null
$Array.Add("Value4") > $null
return $return
}
$display = Array-Fix1
write-host -ForegroundColor Magenta $display
Output
ReturnValue
or
function Array-Fix2()
{
$Array = New-Object System.Collections.ArrayList
$Return = "ReturnValue"
$Array.Add("Value1") | Out-Null
$Array.Add("Value2") | Out-Null
$Array.Add("Value3") | Out-Null
$Array.Add("Value4") | Out-Null
return $return
}
$display = Array-Fix2
write-host -ForegroundColor Magenta $display
Output
ReturnValue