Showing posts with label Strings. Show all posts
Showing posts with label Strings. Show all posts

Monday, January 9, 2012

Creating Multiline Strings

 

PowerTip of the Day, from PowerShell.com:

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

(...)

 

Thanks