Created 2019-07-20 Sat 10:30

August 4, 2024 · View on GitHub

#+TITLE: Get-ChildItemColor #+AUTHOR: Joon Ro

=Get-ChildItemColor= provides colorization of outputs of Get-ChildItem Cmdlet of PowerShell. It is based on Tim Johnson's [[http://tasteofpowershell.blogspot.com/2009/02/get-childitem-dir-results-color-coded.html][script,]] another [[http://mow001.blogspot.com/2006/01/colorized-msh-ls-replacement.html][script]] by [[http://thepowershellguy.com/][the PowerShell Guy]], and [[https://github.com/Davlind/PSColor][PSColor]].

It provides two main functionalities:

  1. Get-ChildItemColor, which adds coloring to the output of Get-ChildItem.
  2. Get-ChildItemColorFormatWide, which is colored version of Get-ChildItemColor | Format-Wide. This uses Write-Host to output coloring, because Get-ChildItemColor | Format-Wide does not allow multiple colors in one line.

=Get-ChildItemColor= has the following features:

  • Both functions support pipelines --- they are pipeline-aware, so they just return untouhed output of Get-ChildItem when used as a part of a pipeline.
  • As of v3.0.0, it no longer overloads Out-Default, and thus does not have unintended consequences.
  • Both functions work as intended inside OneDrive directories.
  • Screenshots ** Get-ChildItemColor [[file:./screenshots/Get-ChildItemColor.png]] ** Get-ChildItemColorFormatWide (=ls= equivalent) [[file:./screenshots/Get-ChildItemColorFormatWide.png]]
  • Install

Then, you can run =Install-Module Get-ChildItemColor=. ** Install from GitHub After cloning the repo or downloading the files, you can put files in =/src= folder into =Get-ChildItemColor= folder under your =PSModulePath= (e.g., =$ENV:UserProfile\Documents\PowerShell\Modules= for PowerShell 6 and later). The =master= branch always contains the latest release version. ** Install from [[https://chocolatey.org][Chocolatey]] The module is available as a [[https://chocolatey.org/packages/get-childitemcolor][Chocolatey package]]. Install it using =choco install get-childitemcolor=.

  • Usage When you import the module:

#+begin_src powershell Import-Module Get-ChildItemColor #+end_src

it provides two functions, =Get-ChildItemColorFormatWide= and =Get-ChildItemColor=.

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):

#+begin_src powershell If (-Not (Test-Path Variable:PSise)) { # Only run this in the console and not in the ISE Import-Module Get-ChildItemColor

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

} #+end_src

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

Both functions have the following optional switches:

  • -File :: Show only files.
  • -Directory :: Show only directories.

Get-ChildItemColor has the following optional switch:

Get-ChildItemColorFormatWide has the following optional switches:

  • -HideHeader :: Supress printing of headers (path on top).
  • -TrailingSlashDirectory :: Add a trailing slash to directory names.

Note that if you want to use these switches as default, you have to define a function. For example,

#+begin_src powershell function Get-ChildItemColorForceWideHHTS { Get-ChildItemColorFormatWide -HideHeader -TrailingSlashDirectory -Force $Args[0] }

Set-Alias ls Get-ChildItemColorForceWideHHTS -option AllScope #+end_src

[fn:pathProfile] Home\[My]Documents\PowerShell\Profile.ps1 or Home\[My ]Documents\PowerShell\Profile.ps1~ or ~Home[My ]Documents\WindowsPowerShell\Profile.ps1

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

** Customizing color One can dynamically change the color scheme for different items, thanks to [[https://github.com/asidlo][asidlo]]'s [[https://github.com/joonro/Get-ChildItemColor/pull/23][contribution]]. See the example below.

#+begin_src powershell

Change color for directories to Blue

$GetChildItemColorTable.File['Directory'] = "Blue"

Change color for executables to Green

ForEach (ExeinExe in GetChildItemColorExtensions['ExecutableList']) { GetChildItemColorTable.File[GetChildItemColorTable.File[Exe] = "Green" } #+end_src ** Adding a new category One can create a new category and assign colors easily like the example below. #+begin_src powershell $GetChildItemColorExtensions['OfficeList'] = @( ".docx", ".pdf", ".pptx", ".xlsx" )

ForEach (ExtensioninExtension in GetChildItemColorExtensions['OfficeList']) { GetChildItemColorTable.File.Add(GetChildItemColorTable.File.Add(Extension, "Green") } #+end_src ** Customizing vertical space You can adjust the vertical spacing using =$Global:GetChildItemColorVerticalSpace=. Default is 1 (PowerShell's default is 2).

#+begin_src powershell $Global:GetChildItemColorVerticalSpace = 1 #+end_src

  • Authors
  • Changelog