Powershell 输出中的字符串连接在一行中,尽管原始字符串未连接-为什么?

Powershell 输出中的字符串连接在一行中,尽管原始字符串未连接-为什么?,powershell,string-concatenation,Powershell,String Concatenation,我的问题与PowerShell 4.0(Win7)有关 目标是从文本文件中获得一个选定的多行字符串以及另一个准备好的文本 我正在使用以下代码: $nl = [System.Environment]::NewLine [string]$text1 = 'Hello World' + $nl $b = (Get-Content textfile.txt | Out-GridView -OutputMode Multiple) textfile.txt的内容: tester 1 tester 2

我的问题与PowerShell 4.0(Win7)有关

目标是从文本文件中获得一个选定的多行字符串以及另一个准备好的文本

我正在使用以下代码:

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

tester 1  
tester 2  
tester 3  
我从GridView中选择测试仪1和测试仪2,然后单击确定
$b的输出在两行中给出了以下(预期)结果:

        $b  
tester 1  
tester 2  
到目前为止,一切顺利

现在,我将把这些字符串组合在一起:

$test = $text1 + $b  
$test的输出为我提供了以下(意外)结果:

        $test  
Hello World  
tester 1 tester 2  
问题: 为什么$test的输出不像$b的输出那样在两个单独的行中

我希望它是:

Hello World  
tester 1  
tester 2

问题解决了。我在以下帖子中找到了解决方案:

我必须这样设置“输出字段分隔符”:

谢谢你,基思·希尔:-)

$OFS = "`r`n"