Add a Line Break to a Powershell Script

PowerShell has its own ways for the output of special characters, like line breaks and tabs. This also applies to breaking long lines of code for better readability. But, the backtick escape character is only required in rare cases.

In general, most programming languages work with the backslash "\" followed by a letter or combination of digits to show a special character. As for PowerShell, it uses the backtick. For example, if you want to break a line in PowerShell, you need to use n or r rather than \n or \r.

Actually, the line feed is caused by 'n. But, in some cases, this might not be sufficient based on where you open the output file. For example, Notepad will ignore line feeds. So, the line feed should be combined with a carriage return.

You only need the backtick if you must break a line between the parameters of a cmdlet or within a string. On the contrary, you can break a line after a pipe, an assignment, a comma, or a semicolon without using a backtick. Those four characters will stay at the end of the line and you can simply continue with the enter key.

The following is an example of how to use PowerShell to add a new line between lines for the text output. Directly use the 'n character.

PS C:\> "string with new line 'n in it"
string with new line
in it

Please note, if you need a carriage return, use `r. For a carriage return and a new line, please use 'r'n.



Leave a reply



Submit