PoshCode Archive  Artifact [088ed69868]

Artifact 088ed698689d265c4c3cf9b0ed02905da641c3461bc627b6c8e400ed4bd15538:

  • File Get-ChildItemColor.ps1 — part of check-in [c00add082f] at 2018-06-10 14:25:42 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: I should probably update it to precompile the regexes at the top so that they aren’t compiled for each item returned by Get-ChildItem, and 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: 2185)

# 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: I should probably update it to precompile the regexes at the top so that they aren’t compiled for each item returned by Get-ChildItem, and 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: 876
# x-archived: 2016-03-04T19:11:27
# x-published: 2009-02-17T05:10:00
#
#
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
  See Also: Get-ChildItem
.ReturnValue
  All objects returned by Get-ChildItem are passed down the pipeline
  unmodified.
.Notes
  NAME:      Get-ChildItemColor
  AUTHOR:    Tojo2000 <tojo2000@tojo2000.com>
#>
  $fore = $Host.UI.RawUI.ForegroundColor

  Invoke-Expression ("Get-ChildItem $args") |
    %{
      if ($_.GetType().Name -eq 'DirectoryInfo') {
        $Host.UI.RawUI.ForegroundColor = 'DarkCyan'
        echo $_
        $Host.UI.RawUI.ForegroundColor = $fore
      } elseif ($_.Name -match '\.(zip|tar|gz|rar)$') {
        $Host.UI.RawUI.ForegroundColor = 'Yellow'
        echo $_
        $Host.UI.RawUI.ForegroundColor = $fore
      } elseif ($_.Name -match '\.(exe|bat|cmd|py|pl|ps1|psm1|vbs|rb|reg)$') {
        $Host.UI.RawUI.ForegroundColor = 'Green'
        echo $_
        $Host.UI.RawUI.ForegroundColor = $fore
      } elseif ($_.Name -match '\.(txt|cfg|conf|ini|csv)$') {
        $Host.UI.RawUI.ForegroundColor = 'Cyan'
        echo $_
        $Host.UI.RawUI.ForegroundColor = $fore
      } else {
        echo $_
      }
    }
}