How to Split Strings by Newlines in PowerShell?

Sometimes, you might need to split a string by newlines while working with strings. PowerShell provides various methods for this. In this PowerShell tutorial, I will explain how to split strings by newlines in PowerShell using various methods.

To split a string by newlines in PowerShell use the -split operator. For example, if you have a string with USA city names separated by newlines, like $cities = “New YorknLos AngelesnChicagonHoustonnPhoenix”, you can split it into an array using $cityArray = $cities -split “n”. This will create an array with each city name as a separate element: New York, Los Angeles, Chicago, Houston, Phoenix.

Method 1: Using the -split Operator

In PowerShell, you can use the -split operator to split a string by newlines in PowerShell. This operator uses a regular expression to divide the string into an array of substrings.

Here is a complete script.

Let us see, you have a string with newlines in it.

$cities = "New York`nLos Angeles`nChicago`nHouston`nPhoenix"

To split this string into an array in PowerShell, you can use the -split operator with the newline character as the delimiter:

$cities = "New York`nLos Angeles`nChicago`nHouston`nPhoenix"
$cityArray = $cities -split "`n"
$cityArray

This will output:

New York
Los Angeles
Chicago
Houston
Phoenix

After executing the above PowerShell script, you can see the output in the screenshot below:

Split Strings by Newlines in PowerShell

Method 2: Using the Split() Method

Here is another method to split strings by new lines in PowerShell is by using the Split() method of the string object.

Here is an example of how to split a string with newlines using the Split() method:

$cities = "New York`nLos Angeles`nChicago`nHouston`nPhoenix"
$cityArray = $cities.Split("`n")
$cityArray

You can see the output in the screenshot below:

powershell split string by newlines

Read Split a String by Word in PowerShell

Method 3: Handle Different Newline Characters

In some cases, strings might contain different newline characters, such as \r\n (Carriage Return + Line Feed) or \n (Line Feed). To handle these variations, you can use a regular expression with the -split operator.

Here is a string where names are separated by different newline characters:

$cities = "New York`r`nLos Angeles`nChicago`r`nHouston`nPhoenix"

To split this string using newlines correctly, use the following regular expression:

$cities = "New York`r`nLos Angeles`nChicago`r`nHouston`nPhoenix"
$cityArray = $cities -split "`r?`n"
$cityArray

This will handle both \r\n and \n newline characters, producing the correct output:

New York
Los Angeles
Chicago
Houston
Phoenix

Method 4: Using [Environment]::NewLine

PowerShell also allows you to use the [Environment]::NewLine property to split strings. This property is useful if you want your script to be cross-platform, as it automatically uses the appropriate newline character for the operating system.

$cities = "New York`r`nLos Angeles`nChicago`r`nHouston`nPhoenix"
$cityArray = $cities -split [Environment]::NewLine
$cityArray

This method ensures that your script works correctly regardless of the newline character used by the operating system.

Conclusion

In this PowerShell tutorial, I have explained how to split a string by newlines in PowerShell using different methods. We saw all the methods with examples.

The easiest method to split a string with newlines in PowerShell is by using the -split Operator.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.