Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 968455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:29:00+00:00 2026-05-16T02:29:00+00:00

I got this colored dir script from http://tasteofpowershell.blogspot.com/2009/02/get-childitem-dir-results-color-coded.html : function ls { $regex_opts =

  • 0

I got this colored dir script from http://tasteofpowershell.blogspot.com/2009/02/get-childitem-dir-results-color-coded.html:

function ls {
  $regex_opts = ([System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Compiled)

  $fore = $Host.UI.RawUI.ForegroundColor
  $compressed = New-Object System.Text.RegularExpressions.Regex('\.(zip|tar|gz|rar)$', $regex_opts)
  $executable = New-Object System.Text.RegularExpressions.Regex('\.(exe|bat|cmd|ps1|psm1|vbs|rb|reg|dll|o|lib)$', $regex_opts)
  $executable = New-Object System.Text.RegularExpressions.Regex('\.(exe|bat|cmd|ps1|psm1|vbs|rb|reg|dll|o|lib)$', $regex_opts)
  $source = New-Object System.Text.RegularExpressions.Regex('\.(py|pl|cs|rb|h|cpp)$', $regex_opts)
  $text = New-Object System.Text.RegularExpressions.Regex('\.(txt|cfg|conf|ini|csv|log|xml)$', $regex_opts)

  Invoke-Expression ("Get-ChildItem $args") |
    %{
      if ($_.GetType().Name -eq 'DirectoryInfo') {
        $Host.UI.RawUI.ForegroundColor = 'DarkCyan'
        $_
        $Host.UI.RawUI.ForegroundColor = $fore
      } elseif ($compressed.IsMatch($_.Name)) {
        $Host.UI.RawUI.ForegroundColor = 'Yellow'
        $_
        $Host.UI.RawUI.ForegroundColor = $fore
      } elseif ($executable.IsMatch($_.Name)) {
        $Host.UI.RawUI.ForegroundColor = 'Red'
        $_
        $Host.UI.RawUI.ForegroundColor = $fore
      } elseif ($text.IsMatch($_.Name)) {
        $Host.UI.RawUI.ForegroundColor = 'Green'
        $_
        $Host.UI.RawUI.ForegroundColor = $fore
      } elseif ($source.IsMatch($_.Name)) {
        $Host.UI.RawUI.ForegroundColor = 'Cyan'
        $_
        $Host.UI.RawUI.ForegroundColor = $fore
      } else {
        $_
      }
    }
}

It works great, but I most of the time I want only the file names, in wide format. So after the invoke-expression call, I added

  Invoke-Expression ("Get-ChildItem $args") |
    %{
      if ($_.GetType().Name -eq 'DirectoryInfo') {
  :
  :
  :
        $_
      }
    } | format-wide -property Name
}

Now I have a bug. Only the colour of the second column is correct; the first item in each column takes the colour of the item in the second column. For example, if I have

> ls

Directory     Program.exe

Then both Directory and Program.exe will be red, even though Directory is supposed to be DarkCyan. How can I correct this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:29:00+00:00Added an answer on May 16, 2026 at 2:29 am

    Rather than twiddling the foreground/background colors of the host in between displaying text to the screen, why don’t you use Write-Host which gives you a bit more control over the displayed text (you can control when newlines are output) e.g.:

    $_ | Out-String -stream | Write-Host -Fore Red
    

    And for the wide listing use, you will need to handle the column formatting yourself unless you want to update the format data XML for the DirectoryInfo/FileInfo types. If you don’t want to do that, then you can write out each name – padded out appropriately – with the desired color. On the last column, set the -NoNewLine param to $false:

    $width =  $host.UI.RawUI.WindowSize.Width
    $cols = 3   
    ls | % {$i=0; $pad = [int]($width/$cols) - 1} `
           {$nnl = ++$i % $cols -ne 0; `
            Write-Host ("{0,-$pad}" -f $_) -Fore Green -NoNewLine:$nnl}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Create a ContainerView that contains a button and override hitTest:withEvent:… May 16, 2026 at 9:57 am
  • Editorial Team
    Editorial Team added an answer If the backup was in the "custom" format (-Fc), you… May 16, 2026 at 9:57 am
  • Editorial Team
    Editorial Team added an answer To transfer binary data wrapped in XML, encode it using… May 16, 2026 at 9:57 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Hi guys I ran the analyser on http://www.websiteoptimization.com/services/analyze/ and I got a number of
From code analysis (Visual studio), I got this warning: Warning 2 CA2000 : Microsoft.Reliability
I got this exception in time of running a web application in java. What
I got this jQuery code which is working just fine. Except on element I
I got this in my Facebook messages telling me to paste this in my
So I got this crazy idea I could make make something cool work. I
I've got this code below, where I spawn several threads, normally about 7, and
I've got this 1 minute long movie that I want to compile into an
I've got this class. .h: #import @class GLEngine; @interface opengl_engineAppDelegate : NSObject { //
I've got this Chrome extension , and it uses jQuery to open a new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.