R Continue String on Next Line

More often, we need to add new line between lines for string output to make output neat and easy to read. Using Windows PowerShell new line can be easily added to text output or variable.

In this blog post, I will explain adding new line in string output or variable, and how to use PowerShell new line ` (backtick) character to add new line.

PowerShell Tip: If you need a PowerShell carriage return, use `r. Use `n to add PowerShell new line. For a carriage return and new line, use `r`n

PowerShell - add newline to string (carriage return)
add PowerShell new line to string (carriage return)

There are different ways to add newline to string or variable in PowerShell using carriage return `r or line continuation character ` at the end of code.

Backtick (`) character is the PowerShell line continuation character. It ensures the PowerShell script continues to a new line.

To use line continuation character in PowerShell, type space at the end of code, use backtick ` and press enters to continue to new line.

Add PowerShell Newline – Using `r carriage return

Use the special character `r for carriage return in PowerShell. The below example shows how to use `r PowerShell carriage return

PS C:\>"Shell `r`nGeek"

In the above example, it add a newline with PowerShell carriage return (`r) and gives output as

Shell Geek

Do you know: How to use the cat command in windows!

PowerShell new line in string output

Use `n to add newline in string output in PowerShell. Below example shows how to use PowerShell new line `n in string output.

PS C:\>"Welcome to Shell Geek `nIts all about PowerShell"

In the above example, using `n in the string, it add newline in string output in PowerShell. The output of the above command as below

Welcome to Shell Geek Its all about PowerShell

Cool Tip: Tail – Get the last lines of a file in PowerShell!

Using PowerShell newline in Command

More often, we have longer PowerShell script commands and we want to beautify them using multiline statements instead of one single line command.

To add newline in the PowerShell script command, use the` (backtick) character at the end of each line to break the command into multiple lines.

In the below example, we want to get free disk space on a local computer name is a single line command which makes it very difficult to read and manage.

We can easily break it using ` (PowerShell backtick character) for line continuation in a given command

Get-CimInstance -ComputerName localhost win32_logicaldisk | where caption -eq "C:" | foreach-object {write " $($_.caption) $('{0:N2}' -f ($_.Size/1gb)) GB total, $('{0:N2}' -f ($_.FreeSpace/1gb)) GB free "}

After using ` (PowerShell backtick character) for a line break in the command, it can be easily read

Get-CimInstance -ComputerName localhost win32_logicaldisk ` | where caption -eq "C:" ` | foreach-object {write " $($_.caption) $('{0:N2}' ` -f ($_.Size/1gb)) GB total, $('{0:N2}' ` -f ($_.FreeSpace/1gb)) GB free "}

As seen in the above example, using the ` line continuation character in PowerShell to continue code in newline.

Cool Tip: Use Test Connection to ping a list of computers in PowerShell!

Add PowerShell newline to Variable

Using PowerShell carriage return `r or `n, you can add PowerShell newline in variable or newline in string output for the variable as below

PS C:\>$strOutput = "Welcome you to Shell Geek `r`nIts all about PowerShell"  PS C:\>$strOutput Welcome you to Shell Geek Its all about PowerShell

In the above PowerShell script to add newline to a variable, we define a variable with string text and added `n to create newline for variable output.

When we print the strOutut variable on the terminal, it gives output with a line break in between the string output.

Cool Tip: Replace text in a string using PowerShell!

Add PowerShell new line with Write-Host

Using `n to add a newline in string output using the PowerShell Write-Host command. Below command prints new line string on the console

PS C:\>Write-Host "Welcome you to Shell Geek `nIts all about PowerShell" Welcome you to Shell Geek Its all about PowerShell

In the above script, using PowerShell Write-Host newline is added with `n and prints the string on the console with new line in text.

Cool Tip: Do you know how to download a zip file in PowerShell?

PowerShell Add New Line to Array

If you want to add new line to array using PowerShell, you have to use the pipe operator to pipe the array variable to Out-String.

Let's consider an example of an Employee array having employee names stored in an array variable as below

$EmployeeArrayList = 'Tom Smith','Adam Strauss','Tim Smith','Gary willy'

$EmployeeArrayList variable contains lists of employees separated by a comma. To print the array line by line or add new line to the array using the below PowerShell script

$EmployeeArrayList = 'Tom Smith','Adam Strauss','Tim Smith','Gary willy'  $empArray = $EmployeeArrayList  | Out-String  Write-Host $empArray

In the first command, we store the list of employees in variable $EmployeeArrayList

The second command uses an array variable and pipes its output to Out-String and stores the result in a temporary $empArray variable.

Third command print array on the console.

The output of the above script in PowerShell to add new line in Array as below

PowerShell Print array multiple lines
PowerShell Print array multiple lines

Cool Tip: PowerShell String Concatenation with Examples!

Conclusion

I hope you found the above article about different ways to add PowerShell newline to variable or string output in PowerShell helpful.

Using PowerShell carriage return and PowerShell newline ` (backtick character) at the end of each line to break the command in multiple lines.

You can also create a PowerShell new line break while defining a string as below

          PS C:\> "Shell >>Geek" Shell Geek

Read more here about how to create multiline string in PowerShell!

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

winnekethold1942.blogspot.com

Source: https://shellgeek.com/how-to-add-newline-to-string-or-variable/

0 Response to "R Continue String on Next Line"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel