PowerShell New Line Tutorial

PowerShell provides 3 methods in order to add a new line to a string, output, or variable. A new line is simply used to create a new line to put some data or output. All of these 3 methods can be used with different commands. In this tutorial, we examine how to use a new line for output, variables, and commands.

New Line with “`n”

A lot of different programming languages use the “\n” characters to create a new line. PowerShell is using a bit modified version where the tick sign is used instead of slash. In the following example, we add multiple new lines into a string which is seed as a single line. By adding multiple new lines the string will be displayed as multi-line.

PS> Write-Host "I`nlike`nwindowstect"
New Line with “`n”

New Line with [Environment]::NewLine

Another method to create a new line is using the [Environment]::Newline object provided by PowerShell natively. Just calling this object creates a new like. For example, we can use this object by assigning it to variables and using the variable for every new line.

PS> $new_line = [Environment]::NewLine
PS> Write-Host "I"$new_line"like"$new_line"windowstect"

Leave a Comment