HOWTO · PowerShell

How to Split a String Into Separate Variables in PowerShell

This tutorial will introduce splitting a string into separate variables in PowerShell. Learn how to use the -split operator, Split() method, and regular expressions for effective string manipulation. Enhance your scripting capabilities and streamline workflows with these essential techniques.

On this page

To assign parts of a string to separate variables in PowerShell, split the string into an array and assign the elements you need. Use the String.Split() method for literal delimiters and the -split operator when the delimiter should be a regular expression.

The examples below show the values produced by each assignment.

Split a string with the String.Split() method

String.Split() is a method on a string. With no separator argument, it divides text at whitespace. The returned value is an array, so use indexes to place its parts into named variables. The first element is at index 0.

Run this example:

$text = "Hello World"
$parts = $text.Split()
$first = $parts[0]
$second = $parts[1]
$first
$second

$record = "North|East|West"
$limitedParts = $record.Split([char[]]@('|'), 2)
$region = $limitedParts[0]
$remaining = $limitedParts[1]
$region
$remaining

The first pair of variables receives Hello and World. The overload that takes a character array and a count lets you choose a literal delimiter and limit the number of returned parts. In the second example, the pipe is the delimiter and the count is two, so $region receives the text before the first pipe and $remaining receives everything after it, including any later pipes. The delimiter is removed from the returned parts.

Pass a character array when you want to split on any of several literal characters. For example, Split([char[]]@(',', ';')) treats a comma or semicolon as a separator. If consecutive separators matter to your data, check how the selected overload handles the resulting adjacent parts and choose a StringSplitOptions value explicitly when needed. The method is useful when delimiters are literal characters and you want the .NET overloads for separators, options, or a maximum number of parts.

Split a string with the -split operator

The -split operator is a concise alternative that uses a regular expression as its delimiter. This makes it convenient to split at a pattern, such as one or more whitespace characters (\s+) or any character from a set. As with the method, the result is an array and can be assigned to separate variables. The optional limit controls the maximum number of parts; text after the final allowed split remains in the last part.

Run this example:

$fruitLine = "apple,banana;cherry|date"
$firstFruit, $remainingFruit = $fruitLine -split '[,;|]', 2
$firstFruit
$remainingFruit

Here the character class [,;|] matches a comma, semicolon, or pipe. The first variable receives apple; the second receives banana;cherry|date because the split count is two. Unlike String.Split(), -split interprets its delimiter as a regular expression, so escape regex metacharacters when you intend to match them literally.

Use -match when you need to test whether a string fits a pattern or capture matching groups. It does not divide a string at delimiter matches; use -split for that task. Choose String.Split() for literal separators and overload-specific options, and choose -split for regex delimiter patterns. In either case, assign the returned array elements to named variables when later commands need the individual values.