How to Add a New Line to Command Output in PowerShell

Rohan Timalsina Feb 23, 2024
  1. Use `N to Add a New Line to Command Output in PowerShell
  2. Using Out Field Separator OFS to Add a New Line to Command Output in PowerShell
  3. Using [Environment]::NewLine to Add a New Line to Command Output in PowerShell
  4. Using Out-String to Add a New Line to Command Output in PowerShell
  5. Conclusion
How to Add a New Line to Command Output in PowerShell

PowerShell includes a set of special character sequences that can customize the output content. The sequences are commonly known as escape sequences. They begin with the backtick character `` ` and are case-sensitive.

Some examples of escape sequences are `0, `a, `b, `e, `f`, `n, `r, etc. Escape Sequences is only interpreted when enclosed in double-quotes " ".

In this article, we explore different techniques to add a new line to command output in PowerShell. Each provides a unique approach to achieving the desired output format, offering flexibility and control over the appearance of PowerShell script outputs.

Use `N to Add a New Line to Command Output in PowerShell

The escape sequence ``n` is commonly used in many programming languages, including PowerShell, to represent a newline character.

When ``n` is encountered within a string, it instructs the interpreter to move the cursor to the beginning of the next line when displaying the text.

In PowerShell, the backtick is used as an escape character to denote special characters within strings.

Write-Host "This is line 1`nThis is line 2"

In this example, we utilize the Write-Host cmdlet to output two lines of text. By employing the ``nescape sequence, we create a new line between“This is line 1”and“This is line 2”`, thereby achieving a clear separation of content.

Output:

add a new line to command output in powershell - output 1

You can use multiple `n characters to add multiple lines.

Write-Host "This is line 1`nThis is line 2`n`n`This is line 3"

We start by outputting "This is line 1", followed by a newline character using the backtick (``n), which instructs PowerShell to move to the next line. Then, we output “This is line 2”` on a new line by again using the newline character.

After that, we include two consecutive newline characters (``n) using backticks, creating a blank line between “This is line 2”and“This is line 3”. Finally, we output “This is line 3”` on a new line.

Output:

add a new line to command output in powershell - output 2

Using Out Field Separator OFS to Add a New Line to Command Output in PowerShell

In PowerShell, the Output Field Separator (OFS) is a special variable that determines how elements in an array are separated when they are converted to a string. By default, PowerShell separates array elements with a space.

However, you can modify the value of the OFS variable to specify a different separator, such as a newline character, which is represented by the escape sequence ``n`.

First, you must define a separator in the OFS variable.

$OFS = "`n`n"
$color = 'red', 'blue', 'green', 'yellow'
"$($color)"

In this code example, we first set the OFS variable to two ``n, indicating that a newline character should be used to separate elements in the output. Then, we define an array called $color` containing four color strings.

When we use the string interpolation syntax "$($color)", PowerShell implicitly converts the array to a string, separating each element with the OFS value, resulting in each color being displayed on a double new line.

Output:

add a new line to command output in powershell - output 3

Using [Environment]::NewLine to Add a New Line to Command Output in PowerShell

In PowerShell, the [Environment]::NewLine method is a convenient way to retrieve the newline character sequence that is appropriate for the current operating system environment. This method returns a string containing the newline sequence (\n for Unix-based systems and \r\n for Windows-based systems).

By utilizing this method, you can ensure that your PowerShell script produces output with the correct newline characters, making it platform-independent and compatible across different environments.

$color = 'red', 'blue', 'green', 'yellow'
$new = [Environment]::NewLine
$color | foreach { "$_$new" }

In this code snippet, we first define an array called $color containing four color strings. Then, we use the [Environment]::NewLine method to retrieve the newline character sequence appropriate for the current environment and store it in the $new variable.

Next, we iterate over each element in the $color array using the foreach loop. Inside the loop, we concatenate each color string with the newline character stored in $new, ensuring that each color is displayed on a new line in the command output.

Output:

add a new line to command output in powershell - output 4

Another example of using [Environment]::NewLine with Sort-Object. The Sort-Object cmdlet helps sort objects by property values in ascending or descending order.

The following command gets the name of all installed programs on the computer, adds a new line to each program’s name, and sorts it.

$color = 'red', 'blue', 'green', 'yellow'
$new = [Environment]::NewLine
$color | Sort-Object | foreach { "$_$_new" }

In this code example, we first define an array called $color containing four color strings. Then, we use [Environment]::NewLine to retrieve the newline character sequence and store it in the $new variable.

Next, we pipe the $color array to Sort-Object to sort the colors alphabetically. Finally, we iterate over each sorted color using foreach, concatenating each color with the newline character stored in $new, resulting in sorted output with each color on a new line.

Output:

add a new line to command output in powershell - output 5

Using Out-String to Add a New Line to Command Output in PowerShell

In PowerShell, the Out-String cmdlet converts the input objects into a single string, which can be useful for formatting command output. By default, Out-String appends a newline character (``n`) to each object, allowing you to easily add new lines to the command output.

This method is particularly helpful when you want to ensure that each item in the output is displayed on a separate line, improving readability and organization.

$color = 'red', 'blue', 'green', 'yellow'
$color | Out-String

In this code snippet, we first define an array called $color containing four color strings. Then, we use the [Environment]::NewLine method to retrieve the newline character sequence appropriate for the current environment and store it in the $new variable.

Next, we iterate over each element in the $color array using the foreach loop. Inside the loop, we concatenate each color string with the newline character stored in $new, ensuring that each color is displayed on a new line in the command output.

Output:

add a new line to command output in powershell - output 6

Conclusion

PowerShell provides several effective methods to add a new line to command output, enhancing readability and organization. Whether using escape sequences like ``n, leveraging the [Environment]::NewLinemethod for platform-independent newline characters, or customizing theOFS` variable for array output formatting, PowerShell offers versatile solutions to meet various scripting needs.

By understanding and implementing these techniques, PowerShell script authors can create more visually appealing and informative outputs, improving the overall user experience and efficiency of their scripts.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website