How To Split a String in PowerShell: Examples and Code update from March 2023

Learn how to split a PowerShell string into smaller parts. Techniques include using the -Split operator and defining a substring.

Brien Posey

March 6, 2023

5 Min Read
orange fruit split into segments
Alamy

When working with strings in PowerShell, you may find that you need to break a string into smaller parts.

There are at least a couple of different ways to accomplish this. The method that you use will depend on the string’s contents and what it is that you want to accomplish.

In this article, I will show you two important 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 who are unfamiliar with the concept of a delimiter, 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 in fact two different pieces of data present: a first name and a last name. The first name is separated from the last name with a space. The space is therefore 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 so long as it is used consistently. For example, comma-separated value 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.

Related:A Beginner’s Guide to Microsoft PowerShell

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 that you must do is to figure out what type of delimiter is used.

If a space is used as a delimiter, the list might look like this:

Apple Grape Cherry Orange

If a comma is used 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

If the string uses a space as the delimiter, splitting the string is easy. 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.

PowerShell screenshot shows how you split a string that uses a space as a delimiter

PowerShell Split 1

Figure 1. This 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 will have to tell PowerShell which character is acting as the delimiter.

To do so, just place the -Split parameter after the string (instead of before) and then enclose the delimiter in quotation marks.

Related:How to Add a New Line in a PowerShell String

For example, suppose that the list of fruits used 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.

PowerShell screenshot shows how to split a string when the delimiter is not a space

PowerShell Split 2

Figure 2. If the string uses anything other than a space as a delimiter, you must specify the delimiting character.

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, there are four elements in the array (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.

PowerShell screenshot shows how to reference any item in a split string

PowerShell Split 3

Figure 3. You can reference any item in a split string.

Define a Substring To Split a String

So, what if you need to split a string, but the string is not delimited? In that type of 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.

Now suppose that 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 as explained above, but let’s pretend that 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 in the same way as some of the 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.

PowerShell screenshot shows how to use a substring to split non-delimited strings

PowerShell Split 4

Figure 4. You can use a substring as a way of splitting non-delimited strings.

About the Author

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

https://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like