Funny thingy about Format-Wide for PowerShell
2017-FEB-15

I had created the following function a while back to display an Array of String in a nice way through columns, and not a long list as the default for Write-Host.

function format-displaylist{
<#
 .DESCRIPTION
  Display an array larger than certain members into nice column(s) for
  easy readability
#>
 param(
  [Parameter(Mandatory=$true,
   ValueFromPipeline=$true)]
  $arrayList,
  [Parameter(Mandatory=$false,
   ValueFromPipeline=$true,
   HelpMessage="Use the name properly of a multi-dimension array")]
  [switch]$displayName
 )
 switch($arrayList.Count){
  {($_ -le 10)}{$column = 1}
  {($_ -gt 10) -and ($_ -le 15)}{$column = 2}
  default{$column = 3}
 }
 $tempArr = @()
 $tempInx = 1
 switch($displayName){
  $false {
   foreach($item in $arrayList){
    $tempArr += "$tempInx) $item"
    $tempInx = $tempInx + 1
   }
  }
  $true {
   foreach($item in $arrayList){
    $tempArr += "$tempInx) " + $item.Name
    $tempInx = $tempInx + 1
   }
  }
 }
 Write-Host ($tempArr | Format-Wide {$_} -Column $column -Force | Out-String)
}

So the issue with that function is that it adds 3 carriage return to the last line, and made the whole thing look ridiculously ugly looking. It bother me, but I didn't have time or willingness to take a look into why it would do that. That is, until today. While writing a new script, I was constantly calling this function and the displaying of the 3 carriage return was driving me mad. I decide to delve in and see what's causing it, and what I can do to resolve it.

After trying numerous other things, I noticed was Out-String, and I thought to myself why I was doing that. I'm not planning to work with the string, I just want to display the string itself. So I replaced:

Write-Host ($tempArr | Format-Wide {$_} -Column $column -Force | Out-String)

with

$tempArr | Format-Wide {$_} -Column $column -Force | Out-Host

and it gave me an output that I'm happy with. There's still return carriage (2), but it's something I can live with.