EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell New Line

PowerShell New Line

Updated March 6, 2023

PowerShell New Line

Definition of PowerShell New Line

PowerShell new line functionality is the way add the new line to the output of the data like storing the multiple lines results in a file, sending the email messages with the multiple lines of the content, or sometimes adding a new line inside the code so that the users can read the code and the output properly by using the different syntax that PowerShell use, and the process of retrieving the output by formatting them into multiple lines using some syntax or functions.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax:

There is no as such syntax for the New Line but we use the specific keyword for the line break like (`) and the carriage (`n) for the new line.

And the method below.

[System.Environment]::NewLine

How do the new line methods work in PowerShell?

By default PowerShell formats the output in the new line always. For example, in the below command to retrieve the service, you will always get each service in the new line.

Get-Service | ft -AutoSize

Output:

PowerShell New Line 1

When you concatenate the string, it joins the string, not in the new line but the same line. See the below string.

"Hi.." + "This is a continue line"

Output:

PowerShell New Line 2

To break the line and start the new line, you need to use the carriage syntax (`n).

"Hi.." + "`nThis is a continue line"

Output:

PowerShell New Line 3

If the line is single and if we add the `n in the middle of it, it also separates the line.

Write-Output "This is the first line. `nThis is the second line. `nThis is the third line"

Output:

PowerShell New Line 4

When you write a program in any editor, sometimes it becomes cumbersome to write the lengthy code in a single line but with the syntax (`) you can break the code into multiple lines into any PowerShell editor.

For example, we have the command below which retrieves the disk information and also performs some

mathematical operation on the disk size as shown below.

Get-CimInstance -ClassName Win32_LogicalDisk | where{$_.DriveType -eq '3'} | Select DeviceID, VolumeName, @{N='TotalSize(GB)'; E={[Math]::Round(($_.Size)/1GB,2)}}, @{N='FreeSpace(GB)'; E={[Math]::Round(($_.FreeSpace)/1GB,2)}}

This is a single line command which retrieves the logical disk information and total size and free size in GB but when we write it in a single line, new programmers cannot understand it properly so better we segregate the command into multiple lines using (`) symbol.

Get-CimInstance -ClassName Win32_LogicalDisk | where{$_.DriveType -eq '3'} `
| Select DeviceID, VolumeName, `
@{N='TotalSize(GB)'; E={[Math]::Round(($_.Size)/1GB,2)}}, `
@{N='FreeSpace(GB)'; E={[Math]::Round(($_.FreeSpace)/1GB,2)}}

The output of the above both commands.

commands

Even when you add the grave accent (`) in the PowerShell console, it also creates a new line for the program and the next line will begin with the double arrow >> means it is waiting for the commands to continue.

Get-CimInstance win32_logicaldisk `

Output:

logical disk

The disk information retrieval command we can write in the console using multiple lines as shown below.

Get-CimInstance -ClassName Win32_LogicalDisk | where{$_.DriveType -eq '3'} `
| Select DeviceID, VolumeName, `
@{N='TotalSize(GB)'; E={[Math]::Round(($_.Size)/1GB,2)}}, `
@{N='FreeSpace(GB)'; E={[Math]::Round(($_.FreeSpace)/1GB,2)}}

Output:

output

Examples

Let us discuss examples of PowerShell New Line.

Example #1: New line using the variable.

If you are using the variables to store the line or a string and when you concatenate multiple variables then you can add the carriage (`n) in the middle to separate them. For example.

Without using the carriage syntax,

$str1 = 'This is the first line'
$str2 = 'This is the second line'
$str1 + $str2

Output:

line

With using the carriage syntax,

$str1 + "`n" + $str2

Output:

str

Example #2: Using the Split() method to create a new line.

When we use the string or any file content and if we need to split the string into multiple lines by using some specific characters then we can use the split method to create a new line. For example,

$str = "This is the First line. This is the second line. This is the third line"
$str

Output:

line 1

We will separate the above string with the split and create a new line as shown below.

$str.Split('.')

Output:

example 2-1

You can separate lines with any syntax or keyword in the split method.

Example #3: Using a string array to create multiple lines.

We can use the string array, the most common technique to create multiple lines. As shown below.
@"
This is the first line
This is the second line
This is the third line
"@

Output:

line 2

Example #4: Using the string formatting method.

We can use the string formatting method to separate the lines from the string or the file content. An example is shown below.

"This is the first line. {0}This is the second line.{0}This is the third line" -f "`n"

Output:

example 4

You can also use the System. Environment class with the method NewLine() to separate the string into multiple lines.

"This is the first line. {0}This is the second line.{0}This is the third line" -f [Environment]::NewLine

The above command will produce the same output as the previous one.

Example #5: Appending the file with the new line.

When you add the content to the file and if you want to add the line to the next line of it. You can use the below command. We have the output.txt file and its content is as below.

Get-Content C:\Temp\output.txt

If we add the content to the file without using escape character, it will join the new line to the last line.

Add-Content C:\Temp\output.txt -Value "This is the fourth line"

Output:

example 5

So we will add the escape character (`n) here to add the new line.

Add-Content C:\Temp\output.txt -Value "`nThis is the fourth line"

Output:

example 5-1

Conclusion

Every programming language like (Java, Python, C#, PowerShell, etc) has a different approach to create a new line and that is helpful because users may need some line breaks between the output and sometimes inside the code and it is always recommended that to use the line breaks or the new line for the output for better readability.

Recommended Articles

This is a Guide to PowerShell New Line. Here we discuss Introduction, syntax, and parameters, examples with code implementation. You may also have a look at the following articles to learn more –

  1. PowerShell Administrator
  2. Powershell Copy File
  3. PowerShell Boolean
  4. PowerShell Sleep
MICROSOFT POWER BI Course Bundle - 8 Courses in 1
34+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
CYBER SECURITY & ETHICAL HACKING Course Bundle - 13 Courses in 1 | 3 Mock Tests
64+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
62+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
KALI LINUX Course Bundle - 6 Courses in 1
20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
WINDOWS POWERSHELL Course Bundle - 7 Courses in 1
 19+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more