Home » PowerShell » PowerShell Split String into Variables

PowerShell Split String into Variables

Using the PowerShell string built-in Split() function or Split operator, you can easily split a string into multiple variables.

Split() or split operator splits the string into multiple substrings or string arrays. We can assign multiple substrings to variables to hold their value.

In this article, we will discuss how to use the PowerShell string Split() function and Split operator to split a string into variables.

Use Split() to Split String into Multiple Variables

PowerShell string built-in Split() function split the string into multiple substrings. We can store the result of the string array into separate variables.

$topic = "ShellGeek PowerShell" 

# Use Split() function to split string and assign to multiple variables
$website,$category = $topic.Split()  

# Print the variable $website
$website

# Print the variable $category
$category

In the above PowerShell script, the $topic stores the string value. We have used the Split() function over $topic to split the string into multiple substrings.

If no values are passed to the Split() function takes white space as default. PowerShell Split() function split the string on space and assigns the results to $website and $category variables.

The output of the above PowerShell script to split a string into separate variables is:

PowerShell split string into variables
PowerShell split string into variables

Cool Tip: How to Split on NewLine in PowerShell!

PowerShell Split String into Variables using Split Operator

Using the Split operator, you can split the string into variables. The Split operator also splits the string by delimiter and returns a string array. We can assign these results to multiple variables.

$topic = "ShellGeek PowerShell" 

# Use Split operator to split string and assign to multiple variables
$website,$category = $topic -Split " "

# Print the variable $website
$website

# Print the variable $category
$category

In the above PowerShell script, the $topic variable contains the string. To split the string into variables, we have used the Split operator and assigned them to $website and $category variables.

The split operator split the string by space and returns a string array and assigns them to separate variables.

The output of the above PowerShell script to break the string into variables by the split operator is:

PS C:\> $topic = "ShellGeek PowerShell"                                                                                 

PS C:\> $website,$category = $topic -split " "                                                                          

PS C:\> $website                                                                                                        
ShellGeek

PS C:\> $category                                                                                                       
PowerShell

PS C:\>                                                                                                                             

Conclusion

I hope the above article on how to split a string into variables using the Split() function or Split operator is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.