Powershell Continue Output on Same 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.
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:
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:
To break the line and start the new line, you need to use the carriage syntax (`n).
"Hi.." + "`nThis is a continue line"
Output:
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:
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.
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:
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:
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:
With using the carriage syntax,
$str1 + "`n" + $str2
Output:
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:
We will separate the above string with the split and create a new line as shown below.
$str.Split('.')
Output:
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:
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:
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:
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:
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 –
- PowerShell Administrator
- Powershell Copy File
- PowerShell Boolean
- PowerShell Sleep
Source: https://www.educba.com/powershell-new-line/
0 Response to "Powershell Continue Output on Same Line"
Post a Comment