How to Convert Multiline String to Array in PowerShell?

Do you want to convert multiline string to array in PowerShell? In this PowerShell tutorial, I will explain how to convert a multiline string into an array using PowerShell using various methods and examples.

To convert a multiline string to an array in PowerShell, you can use the -split operator with the newline character “n”or theSplit()method. For example,$array = $multiLineString -split “n” splits the string at each newline into an array of lines. This method transforms a multiline string into an array where each line becomes an element in the array.

Convert Multiline String to Array in PowerShell

Let’s define what we mean by a multiline string and an array in PowerShell.

A multiline string is a string that spans over multiple lines. Here’s an example:

$multiLineString = @"
Line one
Line two
Line three
"@

An array in PowerShell is a data structure that holds a collection of items. The items can be of any type, including strings, integers, and even other arrays. Here’s an example of a string array:

$stringArray = @("Line one", "Line two", "Line three")

Now, let’s look at how to convert a multiline string into an array in PowerShell.

Method 1: Using the -split Operator

The simplest way to convert a multiline string into an array in PowerShell is by using the -split operator. This operator splits a string into substrings based on a delimiter. By default, if you don’t specify a delimiter, PowerShell will split the string at each newline character.

Here’s an example:

$multiLineString = @"
Line one
Line two
Line three
"@

$array = $multiLineString -split "`r`n" # or -split "`n" on *nix systems

In this example, the -split operator is used to split the string at each carriage return and newline character ("rn”). On Unix-like systems, you might only need to split on the newline character (`”n”).

Check out the screenshot below for the output:

Convert Multiline String to Array in PowerShell

Method 2: Using the .Split() Method

Another way to convert a multiline string into an array is by using the .Split() method directly on the string object. This method allows you to specify the delimiter as an argument.

Here’s how you can use it:

$multiLineString = @"
Line one
Line two
Line three
"@

$array = $multiLineString.Split("`r`n", [StringSplitOptions]::RemoveEmptyEntries)

In this example, the .Split() method is called on the $multiLineString variable, and it’s told to split the string at each carriage return and newline character. The [StringSplitOptions]::RemoveEmptyEntries argument is used to remove any empty entries that could be created if there are consecutive newline characters.

Method 3: Using the ConvertFrom-String Cmdlet

PowerShell also provides a cmdlet named ConvertFrom-String that can be used to convert structured string data into objects. This cmdlet is a bit more advanced and can be used for complex string parsing.

Here’s a simple use case:

$multiLineString = @"
Line one
Line two
Line three
"@

$template = @{
    Delimiter = "`r`n"
}
$array = ($multiLineString | ConvertFrom-String -TemplateContent $template).P1

In this example, ConvertFrom-String is used with a template that specifies the delimiter. The .P1 property is then accessed to get the resulting array.

Method 4: Using a Custom Function

If you find yourself converting multiline strings to arrays frequently, you might want to create a custom function to simplify the task.

Here’s an example of a custom function:

function Convert-MultilineStringToArray {
    param (
        [Parameter(Mandatory=$true)]
        [string]$String
    )

    return $String -split "`r`n"
}

$multiLineString = @"
Line one
Line two
Line three
"@

$array = Convert-MultilineStringToArray -String $multiLineString

This function, Convert-MultilineStringToArray, takes a multiline string as input and returns an array by splitting the string at each newline character.

Conclusion

Converting a multiline string into an array in PowerShell is a common task that can be achieved using different methods. Whether you prefer using the -split operator, the .Split() method, the ConvertFrom-String cmdlet, or creating a custom function, PowerShell provides the flexibility to handle this.

In this PowerShell tutorial, I have explained everything about how to convert a multiline string into an array in PowerShell.

You may also like: