包含PowerShell中文本颜色的变量

包含PowerShell中文本颜色的变量,powershell,variables,text,colors,write-host,Powershell,Variables,Text,Colors,Write Host,是否可以在变量中存储文本的颜色?我试过这个,但不起作用: $format1 = "-ForegroundColor White" $format2 = "-BackgroundColor Black" Write-Host "Example" $format1 $format2 它返回: "Example -ForegroundColor White -BackgroundColor Black" #(not c

是否可以在变量中存储文本的颜色?我试过这个,但不起作用:

$format1 = "-ForegroundColor White"
$format2 = "-BackgroundColor Black"

Write-Host "Example" $format1 $format2
它返回:

"Example -ForegroundColor White -BackgroundColor Black" #(not colored)

以下是你如何完成你想要达到的目标

$format1 = @{
    ForegroundColor="White"
    BackgroundColor="Black"
}

Write-Host "Example" @format1

此方法被调用,基本上就是使用
哈希表将多个参数传递给函数的方法

$cyan=[System.ConsoleColor]::cyan