![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
| | #7 (permalink) |
| | 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 Specs![]() |
| | #8 (permalink) |
| | 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 Specs![]() |
| | #9 (permalink) |
| | 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 Specs![]() |
| | #10 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||