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

1 Answer 1

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 :-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.