Are you required to convert a multiline string to a single line in PowerShell? In this tutorial, I will show you how to convert multiline strings to single line in PowerShell using various methods.
To convert a multiline string to a single line in PowerShell, you can use the -replace operator to replace newline characters with spaces, like so: $singleLineString = $multilineString -replace “rn”, ” “. Alternatively, you can split the string into an array using -split and then join it back into a single line with -join: $singleLineString = ($multilineString -split “rn”) -join ” “.
Let us check out all these methods with examples.
Method 1: Using -replace Operator
In PowerShell, you can use the -replace operator to manipulate strings. Here, you can replace newline characters with spaces in a multiline string or any other character you choose.
Here is a complete example.
# Define a multiline string
$multilineString = @"
This is line one.
This is line two.
This is line three.
"@
# Replace newline characters with spaces
$singleLineString = $multilineString -replace "`r`n", " "
# Output the result
Write-Output $singleLineStringIn this example, the -replace operator replaces each occurrence of the newline characters ("rn") with a space, resulting in a single line string.
You can see the output in the screenshot below:

Read How to Filter Strings in PowerShell?
Method 2: Using -join Operator
The -join operator is another approach in PowerShell that can concatenate elements of an array into a single string. By splitting the multiline string into an array and then joining it, you can convert a multiline string to a single string in PowerShell.
Here is another example and the complete script.
# Define a multiline string
$multilineString = @"
This is line one.
This is line two.
This is line three.
"@
# Split the string into an array and join it into a single line
$singleLineString = ($multilineString -split "`r`n") -join " "
# Output the result
Write-Output $singleLineStringHere, the -split operator splits the multiline string into an array of lines, and the -join operator concatenates them into a single line with spaces in between.
After I executed the above PowerShell script, you can see the output in the screenshot below.

Method 3: Using Get-Content with -Raw Parameter
When reading from a file, you can use the Get-Content cmdlet with the -Raw parameter to read the entire content as a single string. This method is particularly useful when dealing with file input.
Here is an example of how to convert multiline string content from a file to a single line in PowerShell.
# Read the content of a file as a single string
$filePath = "C:\MyFolder\MyFile.txt"
$singleLineString = Get-Content -Path $filePath -Raw
# Replace newline characters with spaces
$singleLineString = $singleLineString -replace "`r`n", " "
# Output the result
Write-Output $singleLineStringIn this example, Get-Content -Raw reads the entire file content as a single string, and then we use the -replace operator to remove newline characters.
Method 4: Custom Function
In the last method, I will show you how to write a custom function in PowerShell to convert a multiline string to a single string.
Here is the complete PowerShell script.
function Convert-MultilineToSingleLine {
param (
[string]$multilineString,
[string]$separator = " "
)
# Replace newline characters with the specified separator
return $multilineString -replace "`r`n", $separator
}
# Define a multiline string
$multilineString = @"
This is line one.
This is line two.
This is line three.
"@
# Convert using the custom function
$singleLineString = Convert-MultilineToSingleLine -multilineString $multilineString
# Output the result
Write-Output $singleLineStringThis function, Convert-MultilineToSingleLine, takes a multiline string and an optional separator as parameters. It replaces newline characters with the specified separator.
Check out the below screenshot it returns the exact output, look the screenshot below:

Conclusion
Any of the above methods can convert a multiline string to a single line in PowerShell. The easiest method is to use the PowerShell —replace Operator.
I have also explained how to write a custom function in PowerShell for converting a multiline string to a single line.
You may also like the following tutorials:
- Split Strings by Newlines in PowerShell
- Split a String by Word in PowerShell
- Split a String and Get the First and Last Element 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.