Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat character is not working in PowerShell

Tags:

powershell

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?

like image 704
Matt Avatar asked May 02 '16 21:05

Matt


1 Answers

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.

like image 86
Mathias R. Jessen Avatar answered Sep 28 '22 00:09

Mathias R. Jessen