Avoid newline in echo

You can use Write-Host:

Write-Host "Hello " -nonewline
Write-Host "There!"

Or from the command line:

PS C:\> Write-Host "Hello " -nonewline ; Write-Host "There!"
Hello There!

Write-Host -n 

works for me in PowerShell 5.


echo is an alias for Write-Output which sends objects to the next command in the pipeline. If all you want to do is display text on the console you can do:

Write-Host "Hi" -NoNewLine

Keep in mind that this is not the same cmdlet as echo|Write-Output.

Write-Output's primary purpose is send objects to the next command in the pipeline where Write-Host's primary purpose is to display text on the console. The reason why you see text on the console using Write-Output is that the PowerShell engine sends everything to Out-Default at the end of the pipeline which sends the incoming to PowerShell's object to text formatting engine.

Here is an example:

Write-Host "hi" | Get-Member

This will produce an error because Write-Host just writes the text to the console and doesn't forward the string to the next command in the pipeline.

Write-Output "hi" | Get-Member

This will display the System.String properties and methods because Write-Output sends the string object to the next object in the pipeline.

  • Write-Host: http://technet.microsoft.com/en-us/library/dd347596.aspx
  • Write-Output: http://technet.microsoft.com/en-us/library/dd315282.aspx

To complement Andy Arismendi's helpful answer:

  • In Bash, echo -n (or its portable equivalent, printf %s) sends its arguments to the data (output) stream, stdout.

  • PowerShell's equivalent to the Unix stdout stream is its success stream, to which output from commands goes by default.
    Note that PowerShell can send objects of any type to the success stream, not just text.

  • Therefore, strictly speaking, the equivalent of Bash's echo -n 'hi', for instance, is just 'hi' in PowerShell - the string is implicitly sent to the success stream.

  • That said, the only way to send a string without a trailing newline to the console is to bypass the success stream, which is what Write-Host -NoNewLine, as discussed in Andy's answer, does.

    • Up to PowerShell v4, Write-Host output couldn't be captured at all, because it simply printed directly to the console, bypassing PowerShell's output streams altogether.

    • Since PowerShell v5, Write-Host writes to a newly introduced stream for writing structured information, number 6, and is effectively an alias for
      Write-Information -InformationAction Continue - see Get-Help Write-Information.

  • When it comes to writing to files, PowerShell v5 added support for the -NoNewline parameter to the following cmdlets (up to v4, writing/appending to a text file would invariably append a trailing newline), but note that -NoNewLine also doesn't place newlines between inputs, should there be multiple:

    • Set-Content / Add-Content
    • Out-File

Workaround for writing output from multiple commands to a single-line string in the output stream:

The workaround is to simply use string interpolation or concatenation or formatting to combine the output from all commands that should form a single line of output text:

For instance, the (rough) equivalent of the following Bash code ...

#!/bin/bash

echo -n 'Today is: '
date

... in PowerShell is:

  • using an expandable string (string interpolation, inside "...", using subexpression operator $(...)):

    "Today is: $(Get-Date)"
    
  • alternatively, using string concatenation, with operator +:

    'Today is: ' + (Get-Date)
    
  • or, using -f, the (string-)format operator:

    'Today is: {0}' -f (Get-Date)
    

All these PowerShell commands sends a single-line string to the success stream, but note that this string itself is then invariably followed by a newline when it prints in the console.

Tags:

Powershell