Managing PSStyle's File Extension Dictionary

A directory listing in PowerShell colourised by the PSANSIRenderingFileInfo feature.

Documentation on PowerShell's experimental PSANSIRenderingFileInfo feature is understandably limited at the moment.

This experiment was added in PowerShell 7.2. This feature adds the $PSStyle.FileInfo member and enables coloring of specific file types.

  • $PSStyle.FileInfo.Directory - Built-in member to specify color for directories
  • $PSStyle.FileInfo.SymbolicLink - Built-in member to specify color for symbolic links
  • $PSStyle.FileInfo.Executable - Built-in member to specify color for executables.
  • $PSStyle.FileInfo.Extension - Use this member to define colors for different file extensions. The Extension member pre-includes extensions for archive and PowerShell files.
Using Experimental Features in PowerShell | Microsoft Docs

Methods

After some trial-and-error in a console, I found several methods offered by [System.Management.Automation.PSStyle+FileExtensionDictionary] that stand out:

Add()

Creates an extension with an associated decoration and appends it to the dictionary for the current session. Add accepts two parameters, both of which are also stringently validated:

  • [System.String] $extension

    The file extension, which must start with a period.

  • [System.String] $decoration

    The string containing ANSI escape sequence(s) to apply, which cannot contain any printable content (you must use double quotes rather than single quotes as a side-effect of this).

Usage Transcript removed@workstation ~ $: $PSStyle.FileInfo Directory : `e[44;1m SymbolicLink : `e[36;1m Executable : `e[32;1m Extension : .zip = "`e[31;1m" .tgz = "`e[31;1m" .gz = "`e[31;1m" .tar = "`e[31;1m" .nupkg = "`e[31;1m" .cab = "`e[31;1m" .7z = "`e[31;1m" .ps1 = "`e[33;1m" .psd1 = "`e[33;1m" .psm1 = "`e[33;1m" .ps1xml = "`e[33;1m" removed@workstation ~ $: $PSStyle.FileInfo.Extension.Add('.xz', "`e[31;1m") removed@workstation ~ $: $PSStyle.FileInfo Directory : `e[44;1m SymbolicLink : `e[36;1m Executable : `e[32;1m Extension : .zip = "`e[31;1m" .tgz = "`e[31;1m" .gz = "`e[31;1m" .tar = "`e[31;1m" .nupkg = "`e[31;1m" .cab = "`e[31;1m" .7z = "`e[31;1m" .ps1 = "`e[33;1m" .psd1 = "`e[33;1m" .psm1 = "`e[33;1m" .ps1xml = "`e[33;1m" .xz = "`e[31;1m" removed@workstation ~ $:

Remove()

Removes an extension and its associated decoration from the dictionary for the current session. Remove accepts one parameter:

  • [System.String] $extension

    The file extension, which must start with a period.

Usage Transcript removed@workstation ~ $: $PSStyle.FileInfo.Extension.Remove('.cab') removed@workstation ~ $: $PSStyle.FileInfo Directory : `e[44;1m SymbolicLink : `e[36;1m Executable : `e[32;1m Extension : .zip = "`e[31;1m" .tgz = "`e[31;1m" .gz = "`e[31;1m" .tar = "`e[31;1m" .nupkg = "`e[31;1m" .7z = "`e[31;1m" .ps1 = "`e[33;1m" .psd1 = "`e[33;1m" .psm1 = "`e[33;1m" .ps1xml = "`e[33;1m" removed@workstation ~ $:

Clear()

Wipes clean the dictionary containing extensions and their decorations for the current session. Accessible through $PSStyle.FileInfo.Extension.Clear()

Usage Transcript removed@workstation ~ $: $PSStyle.FileInfo.Extension.Clear() removed@workstation ~ $: $PSStyle.FileInfo Directory : `e[44;1m SymbolicLink : `e[36;1m Executable : `e[32;1m Extension : removed@workstation ~ $:

Miscellany

By the way, you do need to execute Enable-ExperimentalFeature PSANSIRenderingFileInfo to make use of any of this. I know I'm stating the obvious, but it never hurts to be thorough!

Magic Spear

Comments