How to Split a String into Variables in PowerShell?

While working on a PowerShell automation script, I was required to split a string into variables. I tried different methods. In this PowerShell script, I will explain multiple ways to split strings into variables in PowerShell using various methods with examples.

To split a string into variables in PowerShell, you can use several methods. The -split operator splits a string into an array based on a specified delimiter, such as $cities -split “, ” for comma-separated values. The Split method of the String class allows for multiple delimiters, e.g., $info.Split(@(“|”, “, “)). For more complex parsing, ConvertFrom-String can transform a delimited string into an object with named properties, and regular expressions with -match can capture specific parts of a string into separate variables.

Split a String into Variables in PowerShell

Here are various methods to split a string into variables in PowerShell using different examples.

1. Using the -split Operator

The -split operator is one of the easiest methods to split strings into variables in PowerShell. It splits a string into an array based on a delimiter you specify.

Here is an example of splitting a string into variables in PowerShell.

$cities = "New York, Los Angeles, Chicago, Houston, Phoenix"
$cityArray = $cities -split ", "
$city1 = $cityArray[0]
$city2 = $cityArray[1]
$city3 = $cityArray[2]
$city4 = $cityArray[3]
$city5 = $cityArray[4]

$city1, $city2, $city3, $city4, $city5

Output:

New York
Los Angeles
Chicago
Houston
Phoenix

In this example, the string is split at each comma followed by a space and stored into variables.

Here you can see the output in the screenshot below:

Split a String into Variables in PowerShell

Read How to Split a String by Word in PowerShell?

2. Using the Split Method

PowerShell also allows you to use the Split method of the String class to split a string and store it into variables.

Here is an example.

$info = "John Doe|123 Main St|New York, NY|10001"
$delimiter = @("|", ", ")
$infoArray = $info.Split($delimiter, [System.StringSplitOptions]::None)
$name = $infoArray[0]
$address = $infoArray[1]
$city = $infoArray[2]
$state = $infoArray[3]
$zip = $infoArray[4]

$name, $address, $city, $state, $zip

You can see the output in the screenshot below after executing the script using VS code.

powershell split string into variables

3. Using ConvertFrom-String

The ConvertFrom-String cmdlet is another method to split a string into variables in PowerShell.

Here is a complete code:

$data = "Name: John Doe, Address: 123 Main St, City: New York, State: NY, Zip: 10001"
$parsedData = $data | ConvertFrom-String -PropertyNames 'Name', 'Address', 'City', 'State', 'Zip'
$name = $parsedData.Name
$address = $parsedData.Address
$city = $parsedData.City
$state = $parsedData.State
$zip = $parsedData.Zip

$name, $address, $city, $state, $zip

You can see the output in the screenshot below after I executed the PowerShell script using VS code.

Split a String into Variables in PowerShell Example

Read How to Split Strings by Newlines in PowerShell?

4. Using Regular Expressions with -match

Here is another example of how to use regular expressions with -match to split a string into variables in PowerShell.

Here is a complete PowerShell script.

$address = "John Doe, 123 Main St, New York, NY 10001"
if ($address -match "^(.*), (.*), (.*), (.*) (\d+)$") {
    $name = $matches[1]
    $street = $matches[2]
    $city = $matches[3]
    $state = $matches[4]
    $zip = $matches[5]
}

$name, $street, $city, $state, $zip

I executed the above PowerShell script, and you can see the output in the screenshot below:

How to Split a String into Variables in PowerShell

Conclusion

In this PowerShell tutorial, I have explained how to split a string into variables in PowerShell using various methods. I hope this helps.

You may 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.