简体   繁体   English

在Powershell结果中为列添加颜色

[英]Add Color to a Column in Powershell Results

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? 如果将它导出到txt文件,它是否可以保留颜色?

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

Thanks 谢谢

Take a look at (Get-Host).UI.RawUI.ForegroundColor. 看看(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. 您可以将颜色保存到XML文件,HTML文件或使用Excel或Word Automation创建支持颜色的相应文件。

Communary.ConsoleExtensions [link] might help you Communary.ConsoleExtensions [link]可能对您有所帮助

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. 要保存颜色输出,您必须保存为保留颜色的格式,如RTF或HTML。 Txt (plain text file) only stores text. Txt(纯文本文件)仅存储文本。

The code below will save your output as an html file. 下面的代码将输出保存为html文件。

$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. ...检查作为表格行的html输出中的行。

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. ...使用RegEx将第二个表格单元格内容替换为绿色的字体标记。 This is a very simple RegEx search & replace that will only color the 2nd column . 这是一个非常简单的RegEx搜索和替换,只会为第二列着色

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM