/Get-ChildItemColor

Add coloring to the output of Get-ChildItem Cmdlet of PowerShell.

Primary LanguagePowerShellMIT LicenseMIT

Get-ChildItemColor

  • This adds colors to the output of Get-ChildItem Cmdlet of PowerShell. It is based on Tim Johnson’s script and another script by the PowerShell Guy.
  • Before version 1.0.0, the script actually had used Write-Host to write colored items on console. Now it just changes $Host.UI.RawUI.ForegroundColor and keep the item object intact. Hence, now one can use pipeline. (e.g., ~Get-ChildItemColor | grep “.git”~).
  • Get-ChildItemColorFormatWide still uses Write-Host for output however. This is because Get-ChildItemColor | Format-Wide does not allow multiple colors in one line. Hence, pipeline does not work with Get-ChildItemColorFormatWide.

Screenshot:

Get-ChildItemColor

./screenshots/Get-ChildItemColor.png

Get-ChildItemColorFormatWide (ls equivalent)

./screenshots/Get-ChildItemColorFormatWide.png

Notes on the code

  • It uses a hash table instead of regular expressions to specify colors
  • Recognize the DictionaryEntry case (e.g., Get-ChildItemColor env:)
  • Color support for | Format-Wide format (ls equivalent) with custom script

Install

Install from PowerShellGallery

PowerShellGet is required, which is included in Windows 10 and WMF5. If you are using PowerShell V3 or V4, you will need to install PowerShellGet.

Then, you can simply run Install-Module Get-ChildItemColor (or Install-Module Get-ChildItemColor -Scope CurrentUser).

Install from GitHub

Clone the repo in your PowerShell Module folder ($ENV:UserProfile\Documents\WindowsPowerShell\Modules).

git clone https://github.com/joonro/Get-ChildItemColor.git

Usage

When you import the module:

Import-Module Get-ChildItemColor

it exposes two functions, Get-ChildItemColor and Get-ChildItemColorFormatWide.

You can add aliases to these functions for convenience. For example, I have the following in my profile[fn:pathProfile] (please do not put this into ISE profile[fn:pathProfileISE] as it does not work in ISE):

#verify this is the console and not the ISE
Import-Module Get-ChildItemColor

Set-Alias l Get-ChildItemColor -option AllScope
Set-Alias ls Get-ChildItemColorFormatWide -option AllScope

So l yields colored output of Get-ChildItem and ls yields colored output of Get-ChildItem | Format-Wide equivalent.

[fn:pathProfile] $Home\[My ]Documents\WindowsPowerShell\Profile.ps1

[fn:pathProfileISE] $Home\[My ]Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

Authors

Changelog

v1.2.2

  • Improve README (#15)
  • Beautify code

v1.2.1

  • PR #13: Fallback to Gray when no OriginalForegroundColor (thanks to mikesigs)
  • PR #12: Fix a typo (thanks to jqly)

v1.2.0

  • Robust to non-file entries (Issue #10)

v1.1.0

  • Revert back to previous implementation of Get-ChildItemColorFormatWide

v1.0.0

  • The script changes $Host.UI.RawUI.ForegroundColor only and keep the item object intact
  • Get-ChildItemColorFormatWide is basically Get-ChildItemColor | Format-Wide

v0.5.3

  • Better performance by reducing if’s
  • Proper printing of DirectoryEntry for FormatWide case

v0.5.2

  • Published on PowerShellGallery
  • Refactoring; separate out two functions

v0.4.2

  • Make it a PowerShell module

v0.4.1

  • Returns vanila Get-Childitem results for DictionaryEntry cases.

v0.4.0

  • Make function names consistent to the PowerShell naming convention (#8)
  • Use parameters more consistently, -Path works with paths with spaces (#3), and -Force works (#9)