Cedric Posted a message on the NG (news group) wanting a function like ls for linux. He wanted color while keeping width. I am sure there are other ways to do this, but what I did was created a script (I will convert to a function later) called Get-ChildItemPretty.ps1. The code is at the bottom, but I wanted to point out a few things.
1) It has 3 Parameters and 1 switch
- Dir: This is the Path you want to List (Defaults to $pwd)
- Wide : How Many Colums Wide (Defaults to 3)
- Pad : For Spacing on the Names (Defaults to 30)
- Recurse : Switch to Recurse
- Passtru : outputs objects
2) By default this colors Folders Blue and Exe’s Red, but you could add what ever colors and extentions you want.
3) I have thought about adding a Sort option to Sort by Name or by Type (what do you think?)
4) You could add -full switch that would return fullname instead of just name.
5) You could add -output switch that would return objects as well for pipeline parsing.
### UPDATED ###
Working with /\/\0\/\/ (mostly MOW really) we came up with a better way using $host.UI
This adds a PassTru to output objects
# Colored wide directory list function
#
# /\/\o\/\/ 2007
# www.thePowerShellGuy.com
function List-Wide
{ param ($dir=
$pwd,
$Column=
3,
[Switch]$PassTru,
[Switch]$recurse)
$origFg =
$host.
ui.
rawui.
foregroundColor
if($recurse)
{
$list =
get-ChildItem $dir -recurse
}
else
{
$list =
get-ChildItem $dir
}
foreach ($Item in $list)
{
Switch ($Item.
Extension)
{
".Exe" {$foregroundColor =
"Yellow"}
".cmd" {$foregroundColor =
"Green"}
".ps1" {$foregroundColor =
"Red"}
".vbs" {$foregroundColor =
"Red"}
Default {$foregroundColor =
$origFg}
}
if ($item.
PSISContainer) {$foregroundColor =
"Blue"}
$max = \
[int\
]($Host.
UI.
RawUI.
WindowSize.
Width /
$Column)
$field =
($item.
name.
padRight($max).
Substring(0,
($Max -1)) +
‘ ‘)
if ($item.
name.
length -gt $max -1)
{
$field =
$field.
Substring(0,
($Max -3)) +
‘\*\*\*’
}
if ($Host.
UI.
RawUI.
WindowSize.
Width –
$host.
UI.
RawUI.
CursorPosition.
X -lt $max)
{
$host.
ui.
WriteLine()
}
$Host.
UI.
Write($foregroundColor,
$host.
ui.
rawui.
BackgroundColor,
$field)
if ($PassTru)
{
$Item
}
}
$Host.
UI.
WriteLine()
}
Old Version
tshell :: Jul.10.2007 ::
Powershell, functions ::
1 Comment »