Don’t do that #3: Multiple Write-Host to add new lines

By | February 24, 2015

Don’t do that: I often see people who use multiple lines “Write-Host” to add new lines on the screen (as Write-Host send the output only to the screen).


Do that: You can use the special character `n for new line:  "`nOutput`n"

The result is the same but requires only one line.

special-character-new-line

Note: You can use Here-String in case you have to deal with multiple special characters:

here-string-powershell


previous-buttonnext-button

4 thoughts on “Don’t do that #3: Multiple Write-Host to add new lines

  1. Pingback: Don't do that #2 : Multiple lines to read variables - Powershell Guru

  2. Pingback: Don't do that #4 : Don't ask the user to write in upper case - Powershell Guru

    1. Steve Renard Post author

      Hi Eric,

      Exactly. Personally, I never use Write-Host (I consider it has many inconvenients and limitations) in any PowerShell code I develop.
      But this is only my own opinion and some beginners prefer to use Write-Host (it is their own choice).

      The most important here is the logical to understand and propose different alternatives (although I sometimes don’t agree with all of them but always good to know they exist).

      Regards,

      Steve

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *