I want to repeat a character in PowerShell. For example:
$test = "this is a test that I want to underline with --------"
Write-Host $test
Write-Host "-" * $test.length
However, the output with the above code is:
This is a test that I want to underline with --------
- * 53
Am I missing something really obvious?
Change:
Write-Host "-" * $test.length
to:
Write-Host $("-" * $test.length)
In argument mode, the parser interprets "-" * $test.length
as three separate expandable strings - enclose them in a subexpression ($()
) to have the entire expression evaluated before it's bound as a parameter argument.
You may want to Get-Help about_Parsing
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With