Avoid newline in echo

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)