How to Force PowerShell to Display Output on a New Line

When displaying PowerShell results, sometimes you want to manually force a new line to help format it.  PowerShell uses the escape character with two codes to do that: `r and `n.

The first code, `r, stands for carriage return.  It sets the current position of the cursor to the first position of the current line.

The second code, `n, stands for new line.  This used to be called a line feed, but has been updated.

Here’s some example code for you.  Note that I used the escape character “`” to drop the code to a new line for reading clarity.  You will notice on the line that begins, “Adding one more blank line,”” that even though it allows me to have the code start on a new line, it does not make the text display on a new line.

$crlf = "`r`n"
$msg = "This text is on the first line." + $crlf + `
"This text is on the second line" + $crlf + $crlf + `
"The third line is blank, and this text is on the fourth line. " + `
"Adding one more blank line before ending." + $crlf
clear-host
$msg

Results:

PowerShell New Line Code Results

PowerShell New Line Code Results

Here’s some trivia for those of you who are not old enough to remember.  Back in the days before everyone had a computer, people used typewriters to display text on paper.  The carriage was the equivalent of today’s cursor.  It would type a letter, then move to the next position.  When you wanted to go to the next line, you would feed the paper through the typewriter to advance it down the page, then return the carriage to the beginning.

The Carriage Return/Line Feed combination is still represented on most keyboards today as the “Enter” key.

 

Comments are closed.