Skip to content

joonro/Get-ChildItemColor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Get-ChildItemColor

Get-ChildItemColor provides colorization of outputs of Get-ChildItem Cmdlet of PowerShell. It is based on Tim Johnson’s script, another script by the PowerShell Guy, and 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

./screenshots/Get-ChildItemColor.png

Get-ChildItemColorFormatWide (ls equivalent)

./screenshots/Get-ChildItemColorFormatWide.png

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 run Install-Module Get-ChildItemColor.

Install from GitHub

After cloning the repo, 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 Chocolatey

The module is available as a Chocolatey package. Install it using choco install get-childitemcolor.

Easiest way to use it in PowerShell 7

Download the sources from github.

Input in Powershell the command $Env:PSModulePath and look in the directories, which are there listed.

In the directory you search for, are already directories like “CimCmdlets”, “Microsoft.PowerShell.Archive” and other. Go to this directory called “Modules” and input in the PowerShell mkdir Get-ChildItemColor Go then in the created directory Get-ChildItemColor and copy inside it the files in the “src” directory of github called “FileInfo.ps1”, “Get-ChildItemColor.psd1”, “Get-ChildItemColor.psm1”, “Get-ChildItemColorTable.ps1”, “MatchInfo.ps1”, “ProcessInfo.ps1”, “PSColorHelper.ps1”, “ServiceController.ps1”

Now input in the Powershell the command $PROFILE It outputs now the file, where the Powershell looks for initialization. If the directory to that file not exists, creating it. And if the file not exists, creating it, too. Now open the file in an text-editor like notepad.exe for example with notepad $PROFILE

Input there the lines

Import-Module Get-ChildItemColor
Set-Alias -Name dir -Value Get-ChildItemColor -Option AllScope
Set-Alias -Name ls -Value Get-ChildItemColorFormatWide

and save it. Now start the PowerShell again. With the command “dir” you have a colord file list. With “ls” it is then in wide mode. And “gci” is still as default the old colorless output. You can also use the outwritten normal commands “Get-ChildItemColor”, “Get-ChildItemColorFormatWide” and “Get-ChildItem”.

Usage

When you import the module:

Import-Module Get-ChildItemColor

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

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 [-HideHeader] [-TrailingSlashDirectory]
}

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

Get-ChildItemColorFormatWide has the following optional switches:

-HideHeader
supress printing of headers (path on top).
-TrailingSlashDirectory
add a trailing slash to directory names.

[fn:pathProfile] $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 asidlo’s contribution. See the example below.

# Change color for directories to Blue
$GetChildItemColorTable.File['Directory'] = "Blue"

# Change color for executables to Green
ForEach ($Exe in $GetChildItemColorExtensions['ExecutableList']) {
    $GetChildItemColorTable.File[$Exe] = "Green"
}

Adding a new category

One can create a new category and assign colors easily like the example below.

$GetChildItemColorExtensions['OfficeList'] = @(
    ".docx",
    ".pdf",
    ".pptx",
    ".xlsx"
)

ForEach ($Extension in $GetChildItemColorExtensions['OfficeList']) {
    $GetChildItemColorTable.File.Add($Extension, "Green")
}

Customizing vertical space

You can adjust the vertical spacing using $Global:GetChildItemColorVerticalSpace. Default is 1 (PowerShell’s default is 2).

$Global:GetChildItemColorVerticalSpace = 1

Authors

Changelog

v2.4.0

  • Add HideHeader switch to Get-ChildItemColorFormatWide (#29)

v2.3.0

  • Better handling of header printout (#41)

v2.2.2

  • Add instructions about adding a new category.

v2.2.1

v2.2.0

  • Fix #27, Display issue with Chinese. (Thanks to shiena)

v2.1.1

  • BUGFIX: Print directory names correctly when -Recurse option is used

v2.1.0

  • Re-organize folder structure

v2.0.0

  • Incorporate PSColor’s implementation of coloring the output of Get-ChildItem.
  • Add $Global:GetChildItemColorVerticalSpace option.

v1.3.1

  • PR #21: Added ReparsePoint (symlink) detection, matched color scheme with Linux (thanks to cmilanf)
  • Make empty lines consistent between Get-ChildItemColor and Get-ChildItemColorFormatWide (Fixes #17)

v1.3.0

  • PR #23: Added customizable color output. (thanks to asidlo)
  • Improve README

v1.2.3

  • Add LICENSE

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)