Section 7.15. Colorize the Output of get-childitem


7.15. Colorize the Output of get-childitem

Use a filter to colorize the output of a file listing. Customize the coloring rules by modifying the hashtable or changing the logic in the filter to color by something different.

     $colors=@{msh = "Red"; exe = "Green"; cmd = "Green"; directory = "Blue" }     $defaultColor = "Gray"     filter color-files {         if ($_.Extension -ne "") { $ext = $_.Extension.Substring(1) }         if ($_.Mode.StartsWith("d")) { $ext = "directory" }         if ($colors.ContainsKey($ext)) { $color = $colors[$ext] }         else { $color = $defaultColor }         write-host -ForegroundColor $color $_     }

After dot sourcing the script, use it in the pipeline after get-childitem to bring color to the console:

     MSH> get-childitem | color-files

Figure 7-1 shows the result.




Monad Jumpstart
Monad Jumpstart
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 117

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net