2

When using the Write-Output command, a new line is automatically appended. How can I write strings to stdout (the standard output) without a newline?

For example:

powershell -command "write-output A; write-output B"

Outputs:

A
B

(Write-Host is no good - it writes data to the console itself, not to the stdout stream)

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
iTayb
  • 12,373
  • 24
  • 81
  • 135
  • possible duplicate of [How to output text without newline in Powershell?](http://stackoverflow.com/questions/3896258/how-to-output-text-without-newline-in-powershell) – wingerse Dec 25 '14 at 12:37
  • @EmpereurAiman Nope, this is not the same question. – iTayb Dec 25 '14 at 12:44
  • Is the newoline bad for where this output is going? Else just build a string or string array of you output to control the newlines `$text = ("This","is","some","words") -join " "; Write-Output $text` – Matt Dec 25 '14 at 14:03

3 Answers3

3

Write-Output writes objects down the pipeline, not text as in *nix for example. It doesn't do any kind of text formatting such as appending newlines, hence no need for newline handling options. I see people very often not coming to grips with this.

If you are referring to the newlines printed on the console output, it's because the pipeline is always eventually terminated by Out-Default, which forwards to a default output target (usually Out-Host), which in turn, if it doesn't receive a formatted input, runs the objects through an appropriate default formatter (usually Format-List or Format-Table). The formatter here is the only one in the process responsible for formatting the output, e.g. printing each object on a new line for the console output.

You can override this default behavior by specifying the formatter of your liking at the end of the pipeline, including your own using Format-Custom.

famousgarkin
  • 13,687
  • 5
  • 58
  • 74
2

Write-Output is not appending the newlines.

Try this:

filter intchar {[int[]][char[]]$_}

'123' | Write-Output | intchar

49
50
51

The filter is converting the string to the ASCII int representation of each character. There is no newline being appended.

Adding a couple of explicit newlines for comparison:

"1`n2`n3" | write-output | intchar

49
10
50
10
51

Now we see the additional newlines between the characters, but still no newline appended to the string.

Not sure what your application is, but if you're getting unwanted newlines in your output, I don't think it's Write-Output that's doing it.

famousgarkin
  • 13,687
  • 5
  • 58
  • 74
mjolinor
  • 66,130
  • 7
  • 114
  • 135
  • Well, that's weird, because `powershell -command "write-output A; write-output B"` contains a newline. Where did it come from? – iTayb Dec 25 '14 at 13:13
  • 4
    The newline is being added by Out-Default, which is automatically appended onto an un-terminated pipeline. http://technet.microsoft.com/en-us/magazine/gg213852.aspx The default for Out-Default is Out-Host. – mjolinor Dec 25 '14 at 13:18
0

mjolinor/famousgarkin explain why the output has a new line that is not itself generated by Write-Output. Simple approach to deal with this is to build your output string with Write-Output

$text = ("This","is","some","words") -join " "; 
$string = Write-Output $text
$string += Write-Output $text
$string

Output

This is some wordsThis is some words
Matt
  • 45,022
  • 8
  • 78
  • 119