Retaining newlines in PowerShell string

前端 未结 2 1140
[愿得一人]
[愿得一人] 2020-12-18 08:37

In a PowerShell script, I\'m capturing the string output of an EXE file in a variable, then concatenating it with some other text to build an email body.

However, w

相关标签:
2条回答
  • 2020-12-18 09:23

    Perhaps this helps:

    $msg = "Output of other.exe: " + "`r`n" + ( (.\other.exe) -join "`r`n")
    

    You get a list of lines and not a text from other.exe

    $a = ('abc', 'efg')
     "Output of other.exe: " + $a
    
    
     $a = ('abc', 'efg')
     "Output of other.exe: " +  "`r`n" + ($a -join "`r`n")
    
    0 讨论(0)
  • 2020-12-18 09:28

    Or you could simply set $OFS like so:

    PS> $msg = 'a','b','c'
    PS> "hi $msg"
    hi a b c
    PS> $OFS = "`r`n"
    PS> "hi $msg"
    hi a
    b
    c
    

    From man about_preference_variables:

    Output Field Separator. Specifies the character that separates the elements of an array when the array is converted to a string.

    0 讨论(0)
提交回复
热议问题