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"
$cityArrayThis will output:
New York
Los Angeles
Chicago
Houston
PhoenixAfter executing the above PowerShell script, you can see the output in the screenshot below:

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")
$cityArrayYou can see the output in the screenshot below:

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"
$cityArrayThis will handle both \r\n and \n newline characters, producing the correct output:
New York
Los Angeles
Chicago
Houston
PhoenixMethod 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
$cityArrayThis 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:
- Split String by Tab Character in PowerShell
- How to Split a String into Variables in PowerShell?
- Split a String and Get the First and Last Element in PowerShell
- How to Filter Strings in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.