How to Split Strings into Arrays in PowerShell?

Do you want to split strings into arrays in PowerShell? In this PowerShell tutorial, I will show you different methods to split strings in PowerShell.

In PowerShell, to split a string into an array, use the -split operator with the string variable. For example, $array = $string -split 'delimiter' splits $string at each occurrence of ‘delimiter’, creating an array $array of the resulting substrings. This method is efficient for parsing strings based on specific separators.

Split Strings into Arrays in PowerShell

Using the -split Operator

The simplest way to split a string in PowerShell is by using the -split operator. This operator allows you to specify a delimiter, which PowerShell will then use to divide the string into an array of substrings.

$stringToSplit = "one,two,three,four"
$array = $stringToSplit -split ","

In this example, the string “one,two,three,four” is split at each comma, creating an array with four elements: “one”, “two”, “three”, and “four”.

Using the .Split() Method

Another way to split a string is to use the .Split() method in PowerShell. This method is a part of the string object in PowerShell and can take a character or an array of characters as its argument.

$stringToSplit = "one two three four"
$delimiter = " "
$array = $stringToSplit.Split($delimiter)

Here, the .Split() method is used to split the string at each space, resulting in an array of words. You can see the screenshot for the output after executing the script using PowerShell ISE.

Split Strings into Arrays in PowerShell

Split Strings into Arrays in PowerShell with Multiple Delimiters

Example using -split operator

Sometimes, you might need to split a string using multiple delimiters in PowerShell. Both the -split operator and the .Split() method can handle this scenario.

$stringToSplit = "one,two;three:four"
$delimiters = ',|;|:'
$array = $stringToSplit -split $delimiters

In this example, the -split operator uses a regular expression pattern that includes multiple delimiters (comma, semicolon, and colon).

Example using .Split() Method

Here is another example of using .split() method to split strings into arrays in PowerShell with multiple delimiters.

$stringToSplit = "one,two;three:four"
$delimiters = @(',', ';', ':')
$array = $stringToSplit.Split($delimiters)

For the .Split() method, you pass an array of characters as delimiters, and the method splits the string at each instance of any of the characters.

Split Strings into Arrays in PowerShell Preserving Empty Entries

Example using -split operator

By default, both -split and .Split() will omit empty substrings from the resulting array in PowerShell. If you want to include these empty substrings, you need to use an additional option.

$stringToSplit = "one,,two,,three,,four"
$array = $stringToSplit -split ",", 0, "SimpleMatch"

The “SimpleMatch” option tells PowerShell to include empty substrings in the array.

Example using .Split() Method

Here is an example of splitting strings into arrays in PowerShell, preserving empty entries.

$stringToSplit = "one,,two,,three,,four"
$delimiter = ","
$array = $stringToSplit.Split($delimiter, [System.StringSplitOptions]::None)

Here, we use the StringSplitOptions.None enumeration value to include empty substrings in the array.

Conclusion

Splitting strings into arrays is a common and straightforward task in PowerShell. Whether you prefer the -split operator for its simplicity and power with regular expressions, or the .Split() method for its direct approach, both are effective tools in your scripting toolbox.

In this PowerShell tutorial, I have explained how to split strings into arrays in PowerShell using various methods.

You may also like: