1

I'm new to PowerShell.

I am outputting to a file and keep getting new lines. I'm not sure how to remove the newline function. e.g.

"<Text> -", <Variable> | Add-Content <FilePath>

gives me:

<text>
<Variable>

What I'm after is

<Text> - <Variable> <Newline>
mjsqu
  • 5,151
  • 1
  • 17
  • 21
harfmt
  • 11
  • 1
  • 2
  • Though I didn't get the actual code you are trying to run, you can try to join the string using `-join` operator. Please share the complete command line that you are trying and also, please use code tag wherever you want to include the code or command line in the question. – SavindraSingh Jul 24 '18 at 02:47
  • 1
    Try replacing `,` with `+` – mjsqu Jul 24 '18 at 02:49

2 Answers2

2

"<Text> -", <Variable>, due to use of , - the array-construction operator - creates a 2-element array, and the elements of an array each become their own line on output (via Add-Content).

In order to create a single string, you have several options:

$var = 'foo'  # sample variable

"<Text> - $var" | Add-Content <FilePath> # string expansion

'{0} - {1}' -f '<Text>', $var | Add-Content <FilePath> # -f, the .NET string-formatting operator

'<Text> - ' + $var | Add-Content <FilePath>  # string concatenation
mklement0
  • 382,024
  • 64
  • 607
  • 775
1

You can try below sample code. This example will help you:

$Test = "test"
"<Text> -", $Test -join "" | Add-Content "Test.txt"
SavindraSingh
  • 878
  • 13
  • 39