Home » PowerShell » PowerShell Split on NewLine

PowerShell Split on NewLine

PowerShell string Split() function splits the string into multiple substrings based on the specified separator. To split on newline in a string, you can use the Split() method with [Environment::Newline].

If you have a string or text file that has a CRLF line separator, you can use the PowerShell string Split method or split operator.

In this article, we will discuss how to split on a new line for a given string or text file using the Split() method or split operator.

PowerShell Split on NewLine for String

If you have a string that contains a multiline string and you want to split on a new line, you can easily do it using the PowerShell string split() method.

$testString = "Welcome to `r`nShellGeek Website"

#Split on new line using Environment::NewLine
$testString.Split([System.Environment]::NewLine,[System.StringSplitOptions]::RemoveEmptyEntries)   

In the above PowerShell script, $testString contains the string that has CRLF ( Carriage Return and Line Feed).

Using the PowerShell string Split() method, we have used [Environment]::NewLine as a delimiter to split the string on the new line.

StringSplitOptions::RemoveEmptyEntries is an optional parameter and removes any empty entry from the result.

The output of the above PowerShell script after using the split() method

Welcome to
ShellGeek Website

You can also use the split operator to split the string on newline with the help of [Environment]::NewLine or `r`n

$testString = "Welcome to `r`nShellGeek Website" 

# Split the newline using `r`n
$teststring -split "`r`n" 

# Split the newline using Environment::NewLine
$teststring -split [System.Environment]::NewLine  

In the above PowerShell script, we have used a couple of ways to split the string on newline using either Environment::NewLine or `r`n

The output of the above script after splitting the string on newline is:

Welcome to
ShellGeek Website

Cool Tip: How to split a string into variables in PowerShell!

PowerShell Split Text File on Newline

You can use the Get-Content cmdlet in PowerShell to split the text file on newline. The Get-Content command by default split on a new line and it returns a string array.

(Get-Content -Path D:\PS\Alias.txt)   

In the above PowerShell script, the Get-Content cmdlet uses the Path parameter to specify the name of the file and its path. It reads the content and split it on the newline by default.

The output of the above PowerShell script after splitting the text file into new line is:

PS C:\> (Get-Content -Path D:\PS\Alias.txt)                                                                             # Alias File
# Exported by : ShellGeek
# Date/Time : 30 July 2022 18:57:47
# Computer : INCORP-EU-117
"foreach","ForEach-Object","","ReadOnly, AllScope"
"%","ForEach-Object","","ReadOnly, AllScope"
"where","Where-Object","","ReadOnly, AllScope"
"?","Where-Object","","ReadOnly, AllScope"
"ac","Add-Content","","ReadOnly, AllScope"
"clc","Clear-Content","","ReadOnly, AllScope"

You can also use the PowerShell string Split() method to split the text file on a new line using [Environment]:: NewLine and return the string array.

# Using the Split method to split the text file on new line
(Get-Content -Path D:\PS\Alias.txt).Split([System.Environment]::NewLine)

# Using the Split operator to split the file on newline
(Get-Content -Path D:\PS\Alias.txt -Raw) -split "`r`n"

Conclusion

I hope the above article on how to split a string on newline or text file on newline is helpful to you.

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

Leave a Comment