title: Powershell colored directory listing is incorrect with format-wide
body: <p>I got this colored dir script from <a href="http://tasteofpowershell.blogspot.com/2009/02/get-childitem-dir-results-color-coded.html" rel="noreferrer">http://tasteofpowershell.blogspot.com/2009/02/get-childitem-dir-results-color-coded.html</a>: </p>
  
  <pre><code>function ls {
    $regex_opts = ([System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Compiled)
  
    $fore = $Host.UI.RawUI.ForegroundColor
    $compressed = New-Object System.Text.RegularExpressions.Regex('\.(zip|tar|gz|rar)$', $regex_opts)
    $executable = New-Object System.Text.RegularExpressions.Regex('\.(exe|bat|cmd|ps1|psm1|vbs|rb|reg|dll|o|lib)$', $regex_opts)
    $executable = New-Object System.Text.RegularExpressions.Regex('\.(exe|bat|cmd|ps1|psm1|vbs|rb|reg|dll|o|lib)$', $regex_opts)
    $source = New-Object System.Text.RegularExpressions.Regex('\.(py|pl|cs|rb|h|cpp)$', $regex_opts)
    $text = New-Object System.Text.RegularExpressions.Regex('\.(txt|cfg|conf|ini|csv|log|xml)$', $regex_opts)
  
    Invoke-Expression ("Get-ChildItem $args") |
      %{
        if ($_.GetType().Name -eq 'DirectoryInfo') {
          $Host.UI.RawUI.ForegroundColor = 'DarkCyan'
          $_
          $Host.UI.RawUI.ForegroundColor = $fore
        } elseif ($compressed.IsMatch($_.Name)) {
          $Host.UI.RawUI.ForegroundColor = 'Yellow'
          $_
          $Host.UI.RawUI.ForegroundColor = $fore
        } elseif ($executable.IsMatch($_.Name)) {
          $Host.UI.RawUI.ForegroundColor = 'Red'
          $_
          $Host.UI.RawUI.ForegroundColor = $fore
        } elseif ($text.IsMatch($_.Name)) {
          $Host.UI.RawUI.ForegroundColor = 'Green'
          $_
          $Host.UI.RawUI.ForegroundColor = $fore
        } elseif ($source.IsMatch($_.Name)) {
          $Host.UI.RawUI.ForegroundColor = 'Cyan'
          $_
          $Host.UI.RawUI.ForegroundColor = $fore
        } else {
          $_
        }
      }
  }
  </code></pre>
  
  <p>It works great, but I most of the time I want only the file names, in wide format. So after the invoke-expression call, I added</p>
  
  <pre><code>  Invoke-Expression ("Get-ChildItem $args") |
      %{
        if ($_.GetType().Name -eq 'DirectoryInfo') {
    :
    :
    :
          $_
        }
      } | format-wide -property Name
  }
  </code></pre>
  
  <p>Now I have a bug. Only the colour of the second column is correct; the first item in each column takes the colour of the item in the second column. For example, if I have</p>
  
  <pre><code>&gt; ls
  
  Directory     Program.exe
  </code></pre>
  
  <p>Then both Directory and Program.exe will be red, even though Directory is supposed to be DarkCyan. How can I correct this?</p>