How to Split a String Into Two Variables in PowerShell

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.
If you have a string of text in Microsoft PowerShell, it's often useful to split the string into multiple variables.
Image Credit: dusanpetkovic/iStock/GettyImages

If you have a string of text in Microsoft PowerShell, it's often useful to split the string into multiple variables. For example, you might have a user's first name and last name separated by a space, or a telephone number beginning with an area code. You can use the built-in PowerShell Split function to do this. Other functions are available to have PowerShell parse a string in other ways.

Advertisement

Understanding Microsoft PowerShell

Video of the Day

PowerShell is a command line tool and programming language provided by Microsoft. It is the replacement to the older Windows and DOS batch scripting tool, which is also still available on most versions of Windows.

Advertisement

Video of the Day

It's often used for combining the output from multiple programs or passing data from one program to another, so it's useful to use it for manipulating strings.

The PowerShell Split Function

If you have a string in PowerShell and want to split it on some character, you can do so using the built-in Split function. Either write s -Split, where s is the variable containing the string, or call s.split. The function will return an array containing each of the elements of the split string, which you can capture in a variable like

Advertisement

$a = s.split()

You can assign these to variables by accessing the individual elements as $a[0], $a[1] and so on or keep them in the array if that is more useful.

Splitting strings is often useful when you need to break user input or the output from another program into its component parts.

Advertisement

PowerShell Split Options

By default, PowerShell will split the string on whitespace characters, including spaces, line breaks and tabs. In some cases, you may want to split the string on another character, such as a comma or hyphen, or a string such as " and."

Advertisement

Advertisement

To do this, make the desired character or string to split around the second argument to the split function, by writing s.split(",") or s -split "," to split on a comma, for example. Whatever character you choose to give to PowerShell as delimiter, or separator, will be omitted from all the result strings.

Advertisement

You can also choose a maximum number of strings to split into by providing this as an additional argument. For example, "a,b,c,d".split(",",2) will give an array with the elements "a" and "b,c,d" while "a,b,c,d".split(",") will give an array with each letter in its own string.

Advertisement

Trimming a String in PowerShell

You can have PowerShell trim a string, or remove excess characters from the start or end, by using the trim function. By default, it will remove whitespace from the beginning and end of the string.

Advertisement

For example, " a ".trim() will give the result "a." You can specify other characters you want to be removed from the string as an argument, so "^$a ".trim(" ^") will give the result "$a."

Advertisement

You can use the functions TrimStart and TrimEnd to only remove characters from the beginning or end of the string.

Advertisement

This is often useful when you have data obtained from a user that typed it into an input box with extra spaces or newlines at the end.

The PowerShell Join Function

As in other programming languages, PowerShell's join function is essentially the opposite of split. It takes multiple strings and combines them together with a given delimiter or with no space at all.

Advertisement

For example, -join ("a","b","c") will give the result "abc," while "a","b","c" -join " " will give the result string "a b c." This can be useful when you have multiple variables that you need to combine into one to store in a file, pass to another command or output to a user.

Advertisement

Advertisement

references