Powershell: Properly coloring Get-Childitem output once and for all Powershell: Properly coloring Get-Childitem output once and for all powershell powershell

Powershell: Properly coloring Get-Childitem output once and for all


I just installed and used https://github.com/Davlind/PSColor which was painless. It supports PSGet so you can install easily with Install-Module PSColor to get it.

Note There is an updated fork of PSColor available as Color: https://www.powershellgallery.com/packages/Color/2.1.0 (Thanks @HackSlash)

The objects aren't transformed so they still support piping. (It's using the New-CommandWrapper mentioned above)

It also supports other things like select-string.

PowerShell Color


Modifying Out-Default is definitely the way to go. Below a - granted, sloppy - example.I'm using New-CommandWrapper from the PowerShell Cookbook.

New-CommandWrapper Out-Default `    -Process {        if(($_ -is [System.IO.DirectoryInfo]) -or ($_ -is [System.IO.FileInfo]))        {if(-not ($notfirst)) {           Write-Host "    Directory: $(pwd)`n"                      Write-Host "Mode                LastWriteTime     Length Name"           Write-Host "----                -------------     ------ ----"           $notfirst=$true           }           if ($_ -is [System.IO.DirectoryInfo]) {           Write-host ("{0,-7} {1,25} {2,10} {3}" -f $_.mode, ([String]::Format("{0,10}  {1,8}", $_.LastWriteTime.ToString("d"), $_.LastWriteTime.ToString("t"))), $_.length, $_.name) -foregroundcolor "yellow" }           else {           Write-host ("{0,-7} {1,25} {2,10} {3}" -f $_.mode, ([String]::Format("{0,10}  {1,8}", $_.LastWriteTime.ToString("d"), $_.LastWriteTime.ToString("t"))), $_.length, $_.name) -foregroundcolor "green" }           $_ = $null        }} 

Example Directory Listing


I have another script which takes care of Format-Wide (ls) case and also has better performance by using dictionaries instead of regex: https://github.com/joonro/Get-ChildItem-Color.