重复字符在PowerShell中不起作用

Mat*_*att 3 powershell

我想在PowerShell中重复一个字符.例如:

$test = "this is a test that I want to underline with --------"
Write-Host $test
Write-Host "-" * $test.length
Run Code Online (Sandbox Code Playgroud)

但是,上面代码的输出是:

This is a test that I want to underline with --------
- * 53
Run Code Online (Sandbox Code Playgroud)

我错过了一些非常明显的东西吗

Mat*_*sen 10

更改:

Write-Host "-" * $test.length
Run Code Online (Sandbox Code Playgroud)

至:

Write-Host $("-" * $test.length)
Run Code Online (Sandbox Code Playgroud)

在参数模式中,解析器将其解释"-" * $test.length为三个单独的可扩展字符串 - 将它们包含在子表达式($())中,以便在将其绑定为参数参数之前对整个表达式进行求值.

你可能想要Get-Help about_Parsing.

  • `write-host ("-" * $test.length)` 应该足够了。 (2认同)