PoshCode Archive  Artifact [046d3e94e2]

Artifact 046d3e94e2b08462c9a8f96de9039572f48822185228e637662e58c78fdf3d65:

  • File Get-ChildItemColor.ps1 — part of check-in [d9bc3eb0cd] at 2018-06-10 14:25:46 on branch trunk — A wrapper for Get-ChildItem with color highlighting for different file types. I was thinking of the Linux ‘ls —color’, but didn’t bother to match up colors or anything. TODO: The ability to sort by type would be nice. Note: you will have to remove the documentation at the beginning of the function if you want to use it with versions prior to v2.0 CTP3. (user: tojo2000 size: 2534)

# encoding: ascii
# api: powershell
# title: Get-ChildItemColor
# description: A wrapper for Get-ChildItem with color highlighting for different file types.  I was thinking of the Linux ‘ls —color’, but didn’t bother to match up colors or anything.  TODO: The ability to sort by type would be nice.  Note: you will have to remove the documentation at the beginning of the function if you want to use it with versions prior to v2.0 CTP3.
# version: 0.1
# type: function
# author: tojo2000
# license: CC0
# function: Get-ChildItemColor
# x-poshcode-id: 878
# x-archived: 2016-03-15T07:04:42
# x-published: 2009-02-17T16:15:00
#
# UPDATE: Pre-compiled the Regexes to increase performance.
#
function Get-ChildItemColor {
<#
.Synopsis
  Returns childitems with colors by type.
.Description
  This function wraps Get-ChildItem and tries to output the results
  color-coded by type:
  Compressed - Yellow
  Directories - Dark Cyan
  Executables - Green
  Text Files - Cyan
  Others - Default
.ReturnValue
  All objects returned by Get-ChildItem are passed down the pipeline
  unmodified.
.Notes
  NAME:      Get-ChildItemColor
  AUTHOR:    Tojo2000 <tojo2000@tojo2000.com>
#>
  $regex_opts = ([System.Text.RegularExpressions.RegexOptions]::IgnoreCase `
      -bor [System.Text.RegularExpressions.RegexOptions]::Compiled)

  $fore = $Host.UI.RawUI.ForegroundColor
  $compressed = New-Object System.Text.RegularExpressions.Regex(
      '\.(zip|tar|gz|rar)$', $regex_opts)
  $executable = New-Object System.Text.RegularExpressions.Regex(
      '\.(exe|bat|cmd|py|pl|ps1|psm1|vbs|rb|reg)$', $regex_opts)
  $text_files = New-Object System.Text.RegularExpressions.Regex(
      '\.(txt|cfg|conf|ini|csv|log)$', $regex_opts)

  Invoke-Expression ("Get-ChildItem $args") |
    %{
      if ($_.GetType().Name -eq 'DirectoryInfo') {
        $Host.UI.RawUI.ForegroundColor = 'DarkCyan'
        echo $_
        $Host.UI.RawUI.ForegroundColor = $fore
      } elseif ($compressed.IsMatch($_.Name)) {
        $Host.UI.RawUI.ForegroundColor = 'Yellow'
        echo $_
        $Host.UI.RawUI.ForegroundColor = $fore
      } elseif ($executable.IsMatch($_.Name)) {
        $Host.UI.RawUI.ForegroundColor = 'Green'
        echo $_
        $Host.UI.RawUI.ForegroundColor = $fore
      } elseif ($text_files.IsMatch($_.Name)) {
        $Host.UI.RawUI.ForegroundColor = 'Cyan'
        echo $_
        $Host.UI.RawUI.ForegroundColor = $fore
      } else {
        echo $_
      }
    }
}