Last Updated on February 25, 2024 by Mathew Diekhake
I want to use Windows PowerShell and add a new line between lines for text output. The new line in the string variable in PowerShell should make my code easier to read. Resolution:
The following tutorial demonstrates how to add a new line in PowerShell string when using a version of the Windows oeprating system.
There are two ways you can add a new line to a PowerShell string or variable: by using the `r
carriage return or the line continuation character `
at the end of code. The backtick symbol, also sometimes referred to as a reversed quotation mark, can be used as a line continuation character by typing space at the end of the code, followed by the backtick `
symbol, and then pressing the Enter key on your keyboard to create the new line. The other method using the `r
carriage return is explained in detail below.
How to Add New Line in Powershell String
You can add a newline to string in PowerShell by using the `n
character. Here is how to do that:
1. Open the Windows Terminal app. See this tutorial for how to open Windows Terminal: How to Open Elevated Windows Terminal as Administrator in Windows 11 [Tutorial]
2. From the Windows PowerShell shell, type your code using the following guidelines:
1. PS C:\>"Here is your code `nAnd its new line"
When using the `n
character, it will be transformed into this:
1. Here is your code
2. And its new line
In conclusion, that is how to add a newline to string in PowerShell.
Related Tutorials
- How to Comment in Batch File [Tutorial]
- How to Use Grep Command in Windows [Tutorial]
- How to List Running Containers in Docker [Tutorial]
- How to Change Bash Colors [Tutorial]
- How to Fix Windows 10 Black Screen with Cursor Before/After Login [Tutorial]
- How to Use Cat Command for Windows [Tutorial]