How To Split a String in PowerShell: Examples and CodeHow To Split a String in PowerShell: Examples and Code
Learn how to split a PowerShell string into smaller parts. Techniques include using the -Split operator and defining a substring.
March 6, 2023
When working with strings in PowerShell, you may need to break a string into smaller parts.
There are at least a couple of different ways to accomplish this. The method will depend on the string’s contents and what you aim to do.
In this article, I will show you two valuable techniques.
Use -Split to Split a String
The first option for splitting a string in PowerShell is to use an operator called -Split. The -Split operator method requires that your string contain a delimiter.
What is a delimiter?
For those unfamiliar with delimiters, a delimiter is simply a character that separates data elements. We see delimiters every day, even if we do not think of them as such.
Here’s a simple example by typing my name:
Brien Posey
While it’s easy to think of the above text as “just a name,” there are two pieces of data: a first name and a last name. A space separates the first and last names. The space is the delimiter in this example.
Note that a space is not the only example of a delimiter. Any character can be used as a delimiter if you use it consistently. For instance, comma-separated value (CSV) files separate individual pieces of data with commas. In that case, the comma is the delimiter. I have also occasionally seen the pound sign used as a delimiter.
To illustrate this, let’s pretend that we have a string containing the names of several different types of fruit and that we need to split the string accordingly. The first thing you must do is figure out the delimiter used.
If a space acts as a delimiter, the list might look like this:
Apple Grape Cherry Orange
If a comma acts as a delimiter, the list might look like this:
Apple,Grape,Cherry,Orange
The string could use a dash as a delimiter, which might look like this:
Apple-Grape-Cherry-Orange
The important thing is to determine what character acts as a delimiter to separate one item from another within the string.
How to split a string that uses a space as the delimiter
Splitting the string is easy if the string uses a space as the delimiter. All you do is enter the -Split parameter before the string.
For example, you might type the following:
$Fruit=”Apple Grape Cherry Orange”-Split $Fruit
You can see the results in Figure 1.
Figure 1. Here is how you split a string that uses a space as a delimiter.
How to split a string when the delimiter is not a space
If your string uses anything other than a space as a delimiter, you must tell PowerShell which character is acting as the delimiter.
To do so, place the -Split parameter after the string (instead of before) and then enclose the delimiter in quotation marks.
For example, suppose that the list of fruits uses a comma as a delimiter. Splitting the string would look like this:
$Fruit=”Apple,Grape,Cherry,Orange”$Fruit -Split “,”
You can see what this looks like in Figure 2.
Figure 2. You must specify the delimiting character if the string uses anything other than a space as a delimiter.
How to reference an item in the split string
When you use -Split to split a string, the split string is stored as an array.
In the fruit example we are using, the array has four elements (Apple, Grape, Cherry, and Orange). PowerShell treats the first item as being in position 0, the second item as being in position 1, and so forth. Hence, if you want to reference Grape by itself, you could type:
$Fruit=”Apple,Grape,Cherry,Orange” -Split “,”$Fruit[1]
You can see what this looks like in Figure 3.
Figure 3. You can reference any item in a split string.
Define a Substring To Split a String
What if you need to split a string, but the string is not delimited? In that situation, you can split the string based on character count.
Before learning how this works, know that PowerShell counts the first character in a string as character zero, not character one.
Suppose we have a string containing “Apple Grape Cherry Orange” and want to extract the word Grape. Sure, you could use the space as a delimiter and split the string, but let’s pretend we can’t use this approach. Another way to get the job done would be to define a substring.
The letter G in Grape is the seventh character in the string. Because PowerShell defines the first character as character zero, the G in Grape will be character six. The word Grape is five characters long. With those two pieces of information, we can extract the word Grape.
Here is what the code looks like:
$Fruit=”Apple Grape Cherry Orange”$Favorite = $Fruit.Substring(6,5)
The first line of code defines the string like in previous examples. The second line of code defines a new variable named $Favorite and sets it equal to a substring of $Fruit. The number 6 indicates that we want to begin the substring at character 6, and the number 5 indicates that we want to capture 5 characters.
You can see the results in Figure 4.
Figure 4. You can use a substring to split non-delimited strings.
About the Author
You May Also Like