Question:
I got this colored dir script from http://tasteofpowershell.blogspot.com/2009/02/get-childitem-dir-results-color-coded.html:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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) 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
%{ 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
1 2 3 4 |
> 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?
Answer:
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.:
1 2 |
$_ | 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:
1 2 3 4 5 6 |
$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} |
Source:
Powershell colored directory listing is incorrect with format-wide by stackoverflow.com licensed under CC BY-SA | With most appropriate answer!
, $regex_opts)
$executable = New-Object System.Text.RegularExpressions.Regex('\.(exe|bat|cmd|ps1|psm1|vbs|rb|reg|dll|o|lib)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
1 |
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
1 |
Then both Directory and Program.exe will be red, even though Directory is supposed to be DarkCyan. How can I correct this?
Answer:
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.:
1 |
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:
1 |
Source:
Powershell colored directory listing is incorrect with format-wide by stackoverflow.com licensed under CC BY-SA | With most appropriate answer!
, $regex_opts)
$executable = New-Object System.Text.RegularExpressions.Regex('\.(exe|bat|cmd|ps1|psm1|vbs|rb|reg|dll|o|lib)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
1 |
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
1 |
Then both Directory and Program.exe will be red, even though Directory is supposed to be DarkCyan. How can I correct this?
Answer:
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.:
1 |
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:
1 |
Source:
Powershell colored directory listing is incorrect with format-wide by stackoverflow.com licensed under CC BY-SA | With most appropriate answer!
, $regex_opts)
$source = New-Object System.Text.RegularExpressions.Regex('\.(py|pl|cs|rb|h|cpp)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
1 |
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
1 |
Then both Directory and Program.exe will be red, even though Directory is supposed to be DarkCyan. How can I correct this?
Answer:
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.:
1 |
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:
1 |
Source:
Powershell colored directory listing is incorrect with format-wide by stackoverflow.com licensed under CC BY-SA | With most appropriate answer!
, $regex_opts)
$text = New-Object System.Text.RegularExpressions.Regex('\.(txt|cfg|conf|ini|csv|log|xml)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
1 |
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
1 |
Then both Directory and Program.exe will be red, even though Directory is supposed to be DarkCyan. How can I correct this?
Answer:
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.:
1 |
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:
1 |
Source:
Powershell colored directory listing is incorrect with format-wide by stackoverflow.com licensed under CC BY-SA | With most appropriate answer!
, $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
1 |
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
1 |
Then both Directory and Program.exe will be red, even though Directory is supposed to be DarkCyan. How can I correct this?
Answer:
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.:
1 |
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:
1 |