Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Dir with Color

Reply
 
Old 07-10-2007   #1 (permalink)
Cedric Tallichet


 
 

Dir with Color

Hello!

I try to create a function like the Linux ls. To do a ls, I use
Get-ChildItem | Format-Wide -Autosize.
The only distinction is that Directory is in "[ ]". But I like to change
color (Red for exe, Blue for Directory, etc.)

Any idea? I've found a function sample using $Host.ui.rawui.foregroundcolor
= Color (or something like that) but this seems not to work with the
Format-Wide.

Thanks you!
Cedric


My System SpecsSystem Spec
Old 07-10-2007   #2 (permalink)
dreeschkind


 
 

RE: Dir with Color

I think you need to write your own Format-Wide function using Foreach-Object,
Write-Host etc.

--
greetings
dreeschkind

"Cedric Tallichet" wrote:

> Hello!
>
> I try to create a function like the Linux ls. To do a ls, I use
> Get-ChildItem | Format-Wide -Autosize.
> The only distinction is that Directory is in "[ ]". But I like to change
> color (Red for exe, Blue for Directory, etc.)
>
> Any idea? I've found a function sample using $Host.ui.rawui.foregroundcolor
> = Color (or something like that) but this seems not to work with the
> Format-Wide.
>
> Thanks you!
> Cedric
>
>

My System SpecsSystem Spec
Old 07-10-2007   #3 (permalink)
Brandon Shell


 
 

Re: Dir with Color

Here is a Sample Script that will do it. One thing about this way is you
lose your ability to pipe.
Write-Host does not pass down the pipe and Write-Output does not do color.

Note:
$count = is how wide
$pad = How much to pad the name (if you have long file names)
$wide = how wide you want

Let me know if you want an explanation of anythin
======= CODE =======
# Get-ChildItemPretty.ps1
Param($path,[int]$count=1,[int]$pad=30,[int]$wide=3,[switch]$recurse)
if($recurse)
{
$list = Get-ChildItem $path -recurse
}
else
{
$list = Get-ChildItem $path
}
foreach($item in $list)
{
if($item.PSIsContainer -eq $true)
{
$color = "red"
if($count -lt $wide){write-Host
($item.name).PadRight($pad) -foregroundcolor $color -nonewline;$count++}
else{write-Host ($item.name).PadRight($pad) -foregroundcolor
$color;$count = 1}
}
elseif($item.Extension -match "exe")
{
$color = "blue"
if($count -lt $wide){write-Host
($item.name).PadRight($pad) -foregroundcolor $color -nonewline;$count++}
else{write-Host ($item.name).PadRight($pad) -foregroundcolor
$color;$count = 1}
}
else
{
$color = "White"
if($count -lt $wide){write-Host
($item.name).PadRight($pad) -foregroundcolor $color -nonewline;$count++}
else{write-Host ($item.name).PadRight($pad) -foregroundcolor
$color;$count = 1}
}
}
======= CODE =======
"Cedric Tallichet" <cedric_tallichet@hotmail.com> wrote in message
news:26461C22-B940-43D6-A9E4-81C66D83954C@microsoft.com...
> Hello!
>
> I try to create a function like the Linux ls. To do a ls, I use
> Get-ChildItem | Format-Wide -Autosize.
> The only distinction is that Directory is in "[ ]". But I like to change
> color (Red for exe, Blue for Directory, etc.)
>
> Any idea? I've found a function sample using
> $Host.ui.rawui.foregroundcolor = Color (or something like that) but this
> seems not to work with the Format-Wide.
>
> Thanks you!
> Cedric


My System SpecsSystem Spec
Old 07-10-2007   #4 (permalink)
Brandon Shell


 
 

Re: Dir with Color

whoops.. I got your colors mixed up. Dirs will be red and EXE will be blue.
Easy to change though.

"Brandon Shell" <tshell.mask@gmail.com> wrote in message
news:OyIQVC0wHHA.3400@TK2MSFTNGP03.phx.gbl...
> Here is a Sample Script that will do it. One thing about this way is you
> lose your ability to pipe.
> Write-Host does not pass down the pipe and Write-Output does not do color.
>
> Note:
> $count = is how wide
> $pad = How much to pad the name (if you have long file names)
> $wide = how wide you want
>
> Let me know if you want an explanation of anythin
> ======= CODE =======
> # Get-ChildItemPretty.ps1
> Param($path,[int]$count=1,[int]$pad=30,[int]$wide=3,[switch]$recurse)
> if($recurse)
> {
> $list = Get-ChildItem $path -recurse
> }
> else
> {
> $list = Get-ChildItem $path
> }
> foreach($item in $list)
> {
> if($item.PSIsContainer -eq $true)
> {
> $color = "red"
> if($count -lt $wide){write-Host
> ($item.name).PadRight($pad) -foregroundcolor $color -nonewline;$count++}
> else{write-Host ($item.name).PadRight($pad) -foregroundcolor
> $color;$count = 1}
> }
> elseif($item.Extension -match "exe")
> {
> $color = "blue"
> if($count -lt $wide){write-Host
> ($item.name).PadRight($pad) -foregroundcolor $color -nonewline;$count++}
> else{write-Host ($item.name).PadRight($pad) -foregroundcolor
> $color;$count = 1}
> }
> else
> {
> $color = "White"
> if($count -lt $wide){write-Host
> ($item.name).PadRight($pad) -foregroundcolor $color -nonewline;$count++}
> else{write-Host ($item.name).PadRight($pad) -foregroundcolor
> $color;$count = 1}
> }
> }
> ======= CODE =======
> "Cedric Tallichet" <cedric_tallichet@hotmail.com> wrote in message
> news:26461C22-B940-43D6-A9E4-81C66D83954C@microsoft.com...
>> Hello!
>>
>> I try to create a function like the Linux ls. To do a ls, I use
>> Get-ChildItem | Format-Wide -Autosize.
>> The only distinction is that Directory is in "[ ]". But I like to change
>> color (Red for exe, Blue for Directory, etc.)
>>
>> Any idea? I've found a function sample using
>> $Host.ui.rawui.foregroundcolor = Color (or something like that) but this
>> seems not to work with the Format-Wide.
>>
>> Thanks you!
>> Cedric

>


My System SpecsSystem Spec
Old 07-10-2007   #5 (permalink)
Brandon Shell


 
 

Re: Dir with Color

btw: I wrote a blog post on this for you.
http://bsonposh.com/modules/wordpress/?p=28

"Cedric Tallichet" <cedric_tallichet@hotmail.com> wrote in message
news:26461C22-B940-43D6-A9E4-81C66D83954C@microsoft.com...
> Hello!
>
> I try to create a function like the Linux ls. To do a ls, I use
> Get-ChildItem | Format-Wide -Autosize.
> The only distinction is that Directory is in "[ ]". But I like to change
> color (Red for exe, Blue for Directory, etc.)
>
> Any idea? I've found a function sample using
> $Host.ui.rawui.foregroundcolor = Color (or something like that) but this
> seems not to work with the Format-Wide.
>
> Thanks you!
> Cedric


My System SpecsSystem Spec
Old 07-11-2007   #6 (permalink)
hecks@hotmail.co.uk


 
 

Re: Dir with Color

On Jul 10, 9:44 pm, "Cedric Tallichet" <cedric_tallic...@hotmail.com>
wrote:
> Hello!
>
> I try to create a function like the Linux ls. To do a ls, I use
> Get-ChildItem | Format-Wide -Autosize.
> The only distinction is that Directory is in "[ ]". But I like to change
> color (Red for exe, Blue for Directory, etc.)
>
> Any idea? I've found a function sample using $Host.ui.rawui.foregroundcolor
> = Color (or something like that) but this seems not to work with the
> Format-Wide.
>
> Thanks you!
> Cedric


Here's a slightly more fast & dirty method that takes advantage of out-
string - it just converts the formatted table to a string, then
streams the result down the pipeline one line at a time:

gci . | format-table | out-string -stream | %{
if ($_[0] -match "d") {write-host $_ -fore cyan}
elseif ($_ -match ".exe") {write-host $_ -fore red}
else {write-host $_}
}

Not much you can do with the result, though. It would certainly be
nice if write-output could be colourized as well.

-Hecks

My System SpecsSystem Spec
Old 07-11-2007   #7 (permalink)
/\/\o\/\/ [MVP]


 
 

Re: Dir with Color

You can preserve the pipline by using $host.ui.rawui instead of the
write-host CmDlet.

for an example see :

http://mow001.blogspot.com/2006/01/c...placement.html

Greetings /\/\o\/\/

"hecks@hotmail.co.uk" wrote:

> On Jul 10, 9:44 pm, "Cedric Tallichet" <cedric_tallic...@hotmail.com>
> wrote:
> > Hello!
> >
> > I try to create a function like the Linux ls. To do a ls, I use
> > Get-ChildItem | Format-Wide -Autosize.
> > The only distinction is that Directory is in "[ ]". But I like to change
> > color (Red for exe, Blue for Directory, etc.)
> >
> > Any idea? I've found a function sample using $Host.ui.rawui.foregroundcolor
> > = Color (or something like that) but this seems not to work with the
> > Format-Wide.
> >
> > Thanks you!
> > Cedric

>
> Here's a slightly more fast & dirty method that takes advantage of out-
> string - it just converts the formatted table to a string, then
> streams the result down the pipeline one line at a time:
>
> gci . | format-table | out-string -stream | %{
> if ($_[0] -match "d") {write-host $_ -fore cyan}
> elseif ($_ -match ".exe") {write-host $_ -fore red}
> else {write-host $_}
> }
>
> Not much you can do with the result, though. It would certainly be
> nice if write-output could be colourized as well.
>
> -Hecks
>
>

My System SpecsSystem Spec
Old 07-11-2007   #8 (permalink)
Brandon Shell


 
 

Re: Dir with Color

I knew of this, but it doesn't do the wide aspect he was looking for, but I
like the way you do the color and I like that it perserves the output. I
think I will combine the two scripts. I will post soon.

"/\/\o\/\/ [MVP]" <oMVP@discussions.microsoft.com> wrote in message
news:0C13921E-93D4-4863-BBCB-E1F135E6C76A@microsoft.com...
> You can preserve the pipline by using $host.ui.rawui instead of the
> write-host CmDlet.
>
> for an example see :
>
> http://mow001.blogspot.com/2006/01/c...placement.html
>
> Greetings /\/\o\/\/
>
> "hecks@hotmail.co.uk" wrote:
>
>> On Jul 10, 9:44 pm, "Cedric Tallichet" <cedric_tallic...@hotmail.com>
>> wrote:
>> > Hello!
>> >
>> > I try to create a function like the Linux ls. To do a ls, I use
>> > Get-ChildItem | Format-Wide -Autosize.
>> > The only distinction is that Directory is in "[ ]". But I like to
>> > change
>> > color (Red for exe, Blue for Directory, etc.)
>> >
>> > Any idea? I've found a function sample using
>> > $Host.ui.rawui.foregroundcolor
>> > = Color (or something like that) but this seems not to work with the
>> > Format-Wide.
>> >
>> > Thanks you!
>> > Cedric

>>
>> Here's a slightly more fast & dirty method that takes advantage of out-
>> string - it just converts the formatted table to a string, then
>> streams the result down the pipeline one line at a time:
>>
>> gci . | format-table | out-string -stream | %{
>> if ($_[0] -match "d") {write-host $_ -fore cyan}
>> elseif ($_ -match ".exe") {write-host $_ -fore red}
>> else {write-host $_}
>> }
>>
>> Not much you can do with the result, though. It would certainly be
>> nice if write-output could be colourized as well.
>>
>> -Hecks
>>
>>


My System SpecsSystem Spec
Old 07-11-2007   #9 (permalink)
Brandon Shell


 
 

Re: Dir with Color

Try this version out. Compliments of mostly MoW (posted here at the bottom
http://bsonposh.com/modules/wordpress/?p=28 )

========= CODE =========
function List-Wide { param
($dir=$pwd,$Columns=5,[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 / $Columns)
$field = ($item.name.padRight($max).Substring(0,($Max -1)) + ' ')
if ($item.name.length -gt $max -1)
{
$field = $field.Substring(0,($Max -3)) + '...'
}
$Host.UI.Write($foregroundColor,$host.ui.rawui.BackgroundColor,$field)
if ($PassTru)
{
$Item
}
}
$Host.UI.WriteLine()
}
========= CODE =========

"Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message
news:ewxopd7wHHA.4384@TK2MSFTNGP02.phx.gbl...
>I knew of this, but it doesn't do the wide aspect he was looking for, but I
>like the way you do the color and I like that it perserves the output. I
>think I will combine the two scripts. I will post soon.
>
> "/\/\o\/\/ [MVP]" <oMVP@discussions.microsoft.com> wrote in message
> news:0C13921E-93D4-4863-BBCB-E1F135E6C76A@microsoft.com...
>> You can preserve the pipline by using $host.ui.rawui instead of the
>> write-host CmDlet.
>>
>> for an example see :
>>
>> http://mow001.blogspot.com/2006/01/c...placement.html
>>
>> Greetings /\/\o\/\/
>>
>> "hecks@hotmail.co.uk" wrote:
>>
>>> On Jul 10, 9:44 pm, "Cedric Tallichet" <cedric_tallic...@hotmail.com>
>>> wrote:
>>> > Hello!
>>> >
>>> > I try to create a function like the Linux ls. To do a ls, I use
>>> > Get-ChildItem | Format-Wide -Autosize.
>>> > The only distinction is that Directory is in "[ ]". But I like to
>>> > change
>>> > color (Red for exe, Blue for Directory, etc.)
>>> >
>>> > Any idea? I've found a function sample using
>>> > $Host.ui.rawui.foregroundcolor
>>> > = Color (or something like that) but this seems not to work with the
>>> > Format-Wide.
>>> >
>>> > Thanks you!
>>> > Cedric
>>>
>>> Here's a slightly more fast & dirty method that takes advantage of out-
>>> string - it just converts the formatted table to a string, then
>>> streams the result down the pipeline one line at a time:
>>>
>>> gci . | format-table | out-string -stream | %{
>>> if ($_[0] -match "d") {write-host $_ -fore cyan}
>>> elseif ($_ -match ".exe") {write-host $_ -fore red}
>>> else {write-host $_}
>>> }
>>>
>>> Not much you can do with the result, though. It would certainly be
>>> nice if write-output could be colourized as well.
>>>
>>> -Hecks
>>>
>>>

>


My System SpecsSystem Spec
Old 07-11-2007   #10 (permalink)
Matthias Tacke


 
 

Re: Dir with Color

Brandon Shell wrote:
> Try this version out. Compliments of mostly MoW (posted here at the
> bottom http://bsonposh.com/modules/wordpress/?p=28 )
>
> ========= CODE =========
> function List-Wide { param
> ($dir=$pwd,$Columns=5,[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 / $Columns)
> $field = ($item.name.padRight($max).Substring(0,($Max -1)) + ' ')
> if ($item.name.length -gt $max -1)
> {
> $field = $field.Substring(0,($Max -3)) + '...'
> }
>
> $Host.UI.Write($foregroundColor,$host.ui.rawui.BackgroundColor,$field)
> if ($PassTru)
> {
> $Item
> }
> }
> $Host.UI.WriteLine()
> }
> ========= CODE =========
>


Hi Brandon.

I like it,
but if $columns * $max doesn't fit $Host.UI.RawUI.WindowSize.Width exactly
the columns don't match.

I think you'll need a column counter to insert a crlf

--
Greetings
Matthias
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
color value .NET General
Can't change Color of pages using color/appearance Vista installation & setup
Not all color names give right color? PowerShell
Color Changes Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46