PowerShell更改输出的颜色

时间:2019-07-09 21:08:46

标签: powershell

条件不起作用,总是“不”

Get-Process| Select-Object Name,VM | 
ForEach { 
if ($_.Name -eq "chrome") { [console]::ForegroundColor="red"; $_; } 
else { [console]::ForegroundColor="white"; $_; }  
[console]::ForegroundColor="white"; }

1 个答案:

答案 0 :(得分:1)

改为使用Write-Host,运行[console]::ForegroundColor会更改所有前景文本的颜色,基本上是所有非详细或错误流的文本。但是,如果改为使用Write-Host,则可以更改每一行。

Get-Process msedge,notepad++,chrome | Select-Object Name | 
ForEach { 
    if ($_.Name -eq "chrome") { 
    write-host -ForegroundColor red $_.Name
        } 
    elseif ($_.Name -eq "msedge"){ 
    write-host -ForegroundColor green $_.Name; 
    }
    else{
    write-host -ForegroundColor white $_.Name; 
    }

}

输出:

enter image description here

相关问题