0

My problem is related to PowerShell 4.0 (Win7).

The goal is to get a chosen multiline string out of a textfile together with another prepared text.

I'm using the following code:

$nl = [System.Environment]::NewLine
[string]$text1 = 'Hello World' + $nl
$b = (Get-Content textfile.txt | Out-GridView -OutputMode Multiple)

Content of textfile.txt:

tester 1  
tester 2  
tester 3  

I choose tester 1 and tester 2 from GridView and click OK
An output of $b gives me the following (expected) result in 2 separate lines:

        $b  
tester 1  
tester 2  

So far, so good

Now I'm putting the strings together with:

$test = $text1 + $b  

An output of $test gives me the following (unexpected) result:

        $test  
Hello World  
tester 1 tester 2  

Question: Why is the output of $test not in 2 separate lines as was the output of $b ??

I wanted it to be:

Hello World  
tester 1  
tester 2
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
LightningJack
  • 47
  • 1
  • 1
  • 6

1 Answers1

1

The problem is solved. I found the solution in the following post:

https://stackoverflow.com/a/4433240/9033281

I had to set the "Output Field Separator" like so:

$OFS = "`r`n"

Thank you, Keith Hill :-)

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
LightningJack
  • 47
  • 1
  • 1
  • 6