PowerShell Split String

The string is a group of words or characters. You may have used many string-type variables, values, and arrays in some programming languages while coding. Many built-in and logical functions are useful to be applied to the strings, i.e., concatenation. Today, we will be discussing some examples to split a string of words into separate parts. For this purpose, we will utilize the keyword “split” on the PowerShell of the Ubuntu 20.04 system. Make sure you have PowerShell installed on your Linux terminal to make use of it. So, let’s start by opening the shell terminal of Ubuntu 20.04 via the shortcut key, i.e., Ctrl+Alt+T. After this, start the PowerShell by using the “pwsh” command in the terminal, and you will get the below output shown in the image.

Example 1:

Let’s use the flag word “split” on a single string word in our first example to see how it works. So, we have to use the single word string, i.e., “Linux” with the flag word “-split” to split this word. As a result, it doesn’t split this single word because we haven’t added any delimiter. Let’s move to the string with more than one word in it.

So, we have applied the same flag word “split” on the string with more than one word in it. The words are separated by space in them. Upon execution, we have got all the three words at the separate lines, i.e., split into new three strings. This means the “split” method is automatically taking space as the delimiter to split a string.

Let’s make use of the delimiter. So, we have utilized the same string of words while using the “split” word after the string to specify the delimiter. The space has been specified as a delimiter, as shown in the image below. After running this code line, we have got the string split into three lines.

Example 2:

Let’s take another example to split a string while using a specified delimiter. So, we have taken another string with no space in between. We have used the colon, i.e., “:” within the words of a string, and will be using it as a delimiter. So, we have used the “split” word after the string to specify a delimiter “:”. After the execution, all three words are split out using the colon sign.

The delimiter can also be displayed on the PowerShell using simple brackets. So, we have used the delimiter “:” with brackets for the same string. As a result, we have got the split string while the delimiter is specified in the output as well as separating each word.

Example 3:

Let’s make use of the “split” way to split variable values, i.e., string. So, we have initialized a variable “var” and applied the “split” flag to separate its words by a delimiter space. As a result, we have got the 4 lines of split string.

Example 4:

While splitting a string, you can also mention the number of chunks or parts you want your string to be split to. So, we have started with initializing a string variable with a string value of 4 words separated by space. Then, we have applied the “split” way specifying the space as a delimiter. After specifying the delimiter, we have specified the number of chunks we wanted our string to be split into, i.e., 2 followed by the command. After executing this code, our string has been split into 2 parts. The first chunk contains the first word of a string, while the second one contains all the remaining words of a string.

To split the string variable into 3 chunks, we have specified 3 in the code and run this line of code. We have got the split string converted into 3 chunks as in the image below.

As we have 4 words in the string variable “var”, so we specified the chunk number as 4. As a result, we have got the string split into 4 parts, as shown in the attached image.

Example 5:

Let’s take the example of applying a “split” method on a mix of characters and numbers. So, we have initialized a variable “var” with mixed values separated by a comma. Then, we have used the split method on this variable and specified the comma as a delimiter. As a result, we have got the mixed string split on the PowerShell of Ubuntu 20.04 as per the output below.

To reverse the chunk position, specify the negative value, i.e., “-4” and get the reverse chunk.

Example 6:

Let’s have another example of splitting out the string of words. Therefore, we have declared a string variable “var” with a lot of words in it. We have applied the “split” method on this variable to get it split on the PowerShell. We have mentioned the character “e” as a delimiter here, as you can see. After running this piece of code, we have got the output like below. The output shows that, on meeting delimiter “e” each time, the string has been split to the next line until the end of a string. This is how the character delimiter can be used.

You can also use more than one character from a string as a delimiter. So, we have used the same string variable to be split. The split method has been applied to this variable. After this, we have specified two characters within the square brackets to be used as a delimiter. After executing this updated code, we have got the result as below. The string has been split into different chunks upon meeting any character from “e” or “o”. Therefore, we have got a long split pattern of a string as compared to the above one.


This article has implemented all the necessary details for splitting a string in PowerShell of Ubuntu 20.04. It contains examples of using single and multi-character delimiters to split a string and using chunk numbers to edit the output of split strings. Therefore, we hope you will find this article best at your service.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.