Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Dir with Color

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 07-10-2007   #1 (permalink)
Cedric Tallichet
Guest


 

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
Guest


 

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
Guest


 

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
Guest


 

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
Guest


 

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
Guest


 

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]
Guest


 

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
Guest


 

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
Guest


 

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
Guest


 

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
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't change Color of pages using color/appearance Nate Vista installation & setup 1 12-24-2007 12:27 AM
off color tstp Vista print fax & scan 3 08-22-2007 05:42 PM
Not all color names give right color? Lucvdv PowerShell 3 11-08-2006 05:59 AM
Color Changes David Hankinson Vista General 6 10-10-2006 04:53 PM
Color Bruno Silva Avalon 7 01-10-2006 03:52 PM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com 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 2005-2008