我想在PowerShell中重复一个字符。例如:
$test = "this is a test that I want to underline with --------"
Write-Host $test
Write-Host "-" * $test.length
但是,上面代码的输出是:
This is a test that I want to underline with --------
- * 53
我错过了一些非常明显的东西吗?
答案 0 :(得分:9)
变化:
Write-Host "-" * $test.length
为:
Write-Host $("-" * $test.length)
在参数模式下,解析器将"-" * $test.length
解释为三个单独的可扩展字符串 - 将它们包含在子表达式($()
)中,以便在将其绑定为参数参数之前对整个表达式进行求值。
您可能需要Get-Help about_Parsing
。