How to Convert Multiline String to Single Line in PowerShell?

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 $singleLineString

In 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:

Convert Multiline String to Single Line in PowerShell

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 $singleLineString

Here, 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.

PowerShell Convert Multiline String to Single Line

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 $singleLineString

In 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 $singleLineString

This 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:

convert multiline string to single line powershell

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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