Add Color to a Column in Powershell Results

北慕城南 提交于 2019-11-30 09:47:18

问题


How can I add a different color to the Name Column. If it's possible will it keep the color if I export it to a txt file?

$time = (Get-Date).AddYears(-2)
Get-ChildItem -Recurse | `Where-Object {$_.LastWriteTime -lt $time} | ft -Wrap 
Directory,Name,LastWriteTime | Out-File spacetest.txt

Thanks


回答1:


Take a look at (Get-Host).UI.RawUI.ForegroundColor. And no, you can not save color to a text file. You can save color into XML file, HTML file or use Excel or Word Automation to create appropriate files that do support color.




回答2:


Communary.ConsoleExtensions [link] might help you

Invoke-ColorizedFileListing C:\Windows -m *.dmp

The above command will colorise file types and highlight dump files.

To save a color output, you would have to save to a format that preserves color, like RTF, or HTML. Txt (plain text file) only stores text.

The code below will save your output as an html file.

$time = (Get-Date).AddYears(-2)
Get-ChildItem -Recurse | Where-Object {$_.LastWriteTime -lt $time} |
Select Directory,Name,LastWriteTime |
ConvertTo-Html -Title "Services" -Body "<H2>The result of Get-ChildItem</H2> " -Property Directory,Name,LastWriteTime |
ForEach-Object {
  if ($_ -like '<tr><td>*') {
    $_ -replace '^(.*?)(<td>.*?</td>)<td>(.*?)</td>(.*)','$1$2<td><font color="green">$3</font></td>$4'
  } else {
    $_
  }
} | Set-Content "$env:TEMP\ColorDirList.html" -Force

The line:

if ($_ -like '<tr><td>*') {

...checks for line in the html output that is a table row.

The line:

$_ -replace '^(.*?)(<td>.*?</td>)<td>(.*?)</td>(.*)','$1$2<td><font color="green">$3</font></td>$4'

...uses a RegEx to replace the 2nd table cell contents with a font tag with the color green. This is a very simple RegEx search & replace that will only color the 2nd column.

And here's another implementation of console only coloring, based on this link

$linestocolor = @(
'CSName         Version        OSArchitecture'
'------         -------        --------------'
'BENDER         6.1.7601       64-bit        '
'LEELA          6.1.7601       64-bit        '
'FRY            6.1.7600       64-bit        '
'FARNSWORTH     6.1.7601       32-bit        '
)


# http://www.bgreco.net/powershell/format-color/
function Format-Color {
    [CmdletBinding()]
    param(
      [Parameter(ValueFromPipeline=$true,Mandatory=$true)]
      $ToColorize
    , [hashtable]$Colors=@{}
    , [switch]$SimpleMatch
    , [switch]$FullLine
    )
  Process {
    $lines = ($ToColorize | Out-String).Trim() -replace "`r", "" -split "`n"
    foreach($line in $lines) {
      $color = ''
      foreach($pattern in $Colors.Keys){
        if     (!$SimpleMatch -and !$FullLine -and $line -match "([\s\S]*?)($pattern)([\s\S]*)") { $color = $Colors[$pattern] }
        elseif (!$SimpleMatch -and $line -match $pattern) { $color = $Colors[$pattern] }
        elseif ($SimpleMatch -and $line -like $pattern) { $color = $Colors[$pattern] }
      }
      if ($color -eq '') { Write-Host $line }
        elseif ($FullLine -or $SimpleMatch) { Write-Host $line -ForegroundColor $color }
        else {
        Write-Host $Matches[1] -NoNewline
        Write-Host $Matches[2] -NoNewline -ForegroundColor $color
        Write-Host $Matches[3]
      }
    }
  }
}

$linestocolor | Format-Color -Colors @{'6.1.7600' = 'Red'; '32-bit' = 'Green'}

# doesn't work...
# (Get-ChildItem | Format-Table -AutoSize) | Format-Color -Colors @{'sql' = 'Red'; '08/07/2016' = 'Green'}
# does work...
Format-Color -ToColorize (Get-ChildItem | Format-Table -AutoSize) -Colors @{'sql' = 'Red'; '08/07/2016' = 'Green'}

return


来源:https://stackoverflow.com/questions/39480656/add-color-to-a-column-in-powershell-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!