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, $city5Output:
New York
Los Angeles
Chicago
Houston
PhoenixIn 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:

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, $zipYou can see the output in the screenshot below after executing the script using VS code.

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, $zipYou can see the output in the screenshot below after I executed the PowerShell script using VS code.

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, $zipI executed the above PowerShell script, and you can see the output in the screenshot below:

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:
- Split String by Tab Character in PowerShell
- How to Split a String in PowerShell?
- How to Concatenate Strings in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.