Creating Multiline Strings

by Dec 14, 2011

You probably know what this line produces:

'Hello' * 12

Right, you get 12 times the string you submitted. If you wanted new lines instead, a minor adjustment will do the job:

,'Hello' * 12

The comma puts the string into an array, and when you multiply arrays, you get additional array elements. Simply use Out-String to turn that into a single multi-line string:

PS> $text = ,'Hello' * 12
PS> $text.GetType().FullName
System.Object[]
PS> $text.Count
12
PS> $text = ,'Hello' * 12 | Out-String
PS> $text.GetType().FullName
System.String
PS> $text
Hello
Hello
Hello
(...)

Twitter This Tip!
ReTweet this Tip!