-1

I have 2 different scripts with line breaks. One of them won't make line breaks without "`n" at the end of each line. See this example with and without the "`n".

enter image description here

But other scripts work without the "`n" tag.

write-host "
This is a test message

This is 2 lines down.
"

Output:

This is a test message

This is two lines down.

What makes them different where they do or don't need the "`n". They are both .ps1 extensions. And, as you can see, they are at the start of the script. Why do some scripts show the line breaks with the "`n" and others don't need it?

21
  • The first one you have double quotes inside double quotes. So in the "`n" the following the backtick is interpreted as a line continuation instead of control n. Commented Aug 5, 2024 at 17:21
  • 3
    In multiline strings, literal newlines are normally always respected, as in your second script. I cannot reproduce your symptom, and I'm also baffled that no initial newline is printed before each Write-Host call's output in your first script. Does your first script contain hidden control characters or is unusual in some other respect? Also, you seem to be using the ISE, so let me offer the standard advice in the next comment: Commented Aug 5, 2024 at 17:57
  • 1
    As an aside: The PowerShell ISE is no longer actively developed and there are reasons not to use it (bottom section), notably not being able to run PowerShell (Core) 7. The actively developed, cross-platform editor that offers the best PowerShell development experience is Visual Studio Code with its PowerShell extension. Commented Aug 5, 2024 at 17:57
  • 1
    @MattWilliamson: The linked blog post is interesting as an academic exercise, but not suited to real-world use, not least due to the limitations of the ISE itself (different character encoding, no support for interactive console applications, stderr output as PowerShell errors, ...), but specifically because the use of an out-of-process runspace involves serialization/deserialization, which is not only slow but results in loss of type fidelity. Indeed, Visual Studio Code is the way forward. Commented Aug 5, 2024 at 22:31
  • 1
    @JukEboX, it's the opposite: the bottom image is the expected behavior. I have no explanation for the top image. As noted, I suggest inspecting the script file for hidden control characters. Commented Aug 7, 2024 at 16:26

3 Answers 3

1

Consider the following 2 lines of code:

Write-Host "im`na`nmultiline`nstring"

and

Write-Host "im
a
multiline
string"

They both produce the same output:

im
a
multiline
string

The first is using the escape sequence `n embedded into a single line string. The 2nd is a string that already includes literal newline characters.


For completeness, same can be achieved with a here string(/heredoc) type syntax:

Write-Host @"
I'm
a
heredoc
multiline
string
"@
Sign up to request clarification or add additional context in comments.

4 Comments

I understand what they are for. I am trying to find out some scripts require the single line string to make line breaks and others don't.
A string is 0 or more characters between pairs of " double quotes " - your first example i count 6 double quotes - so your not dealing with a single string. Powershell is very relaxed in its syntax, so this isnt treated as an error, its perfectly valid to concatenate multiple strings together - thats the difference. Another syntax that might be useful to know is the herestring - devblogs.microsoft.com/scripting/…
+1 for the general information (the OP's symptoms are a mystery); a few quibbles: it is the newline format of the enclosing file that determines the newline format of multiline string literals, so it may be `n or `r`n. PowerShell's syntax is only relaxed in argument mode, and even there string concatenation doesn't always work as you'd expect; e.g. "foo"`n"bar", when passed to a command, is two arguments, "foo" and `n"bar", a fact obscured by Write-Host.
@MisterSmith I have tried the splat method and this does not work. It makes all the text on one line still.
0

The only way I can reproduce it is to use backquotes. The output ends up on one line.

write-host `
hi `
there

Output:

hi there

Comments

0

So after working with some friends here we found that issue seems to be the way that ISE reads the file when it is made. If you make a new .ps1 file on ISE it does not notice the script items like Write-Host or functions as a full block of function. This would be denoted by the collapse (+/-) symbol next to that line of code. This means you need to add the `n code to end of each Write-Host line break for it to identify it.

Example: This is a .ps1 file started and made in ISE:

File started in ISE

Now the easiest way to fix this is to take the code and start it over in a Notepad file. Copy your code into a new file on Notepad and then save it as a .ps1. When you do this your Write-Host line breaks will not require the `n as it is being read by ISE correctly.

Example: This is a .ps1 file started and made in Notepad:

File started in Notepad

I am not sure why there is a there is an error with ISE reading .ps1 files when made in ISE and reading line breaks with Write-Host. It may have something to do with no longer being supported. Suggestion would be to move over to VS code with Powershell and code with that. Very strange issue but glad I understand now.

3 Comments

ISE works for me. I assume there's a closing quote at the end.
I also cannot reproduce this: The icon for collapsing the statement appears right away when I type or paste a Write-Host statement with a multiline string into a new document in the ISE, and running the script (even before saving it) behaves as expected. This is on Windows 11, with ISE file version 10.0.22621.1
which version of ISE are you using? I still can't reproduce

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.