Powershell

PowerShell split operator | Explained

PowerShell has a number of built-in functionalities that can be used for different purposes. The Split operator is also one of the PowerShell functions. In PowerShell, the Split operator is used for splitting the strings and substrings into different parts. The split operator offers a variety of parameters to manipulate the strings. This article aims to explain the core concept and usage of the PowerShell split operator.

How to use PowerShell split operator?

The Split operator is used for splitting the statements etc. It can be used with different parameters. Some of the split functions will be explained with different examples in below reading material.

The following examples will explain the concept of the PowerShell split operator.

Example 1: Split a string in PowerShell

Primarily, the split operator breaks down a string into possible substrings wherever the whitespace occurs. In the following code, a string is declared and stored in a variable named “$color”.

$color = "Red, Blue, Green, Yellow, Pink, Orange"

Now, apply the split operator on the variable “$color”.

-split $color

As you can see in the result, all the values in $color have been split into pieces where the whitespace occurred.

Example 2: Split a string with a user-defined delimiter

By default, the Split operator considers whitespace to break down the string. However, you can specify a delimiter of your own choice. For instance, the following code splits the string where the colon(:) is encountered:

Note: The possible delimiters could be comma(,), whitespace, colon(:), semi-colon(;), or any character as well.

"Name:Designation:JoiningDate:Address" -split ":"

As you can observe from the above output, the split operator breaks the string where the “colon(:)” is encountered.

Example 3: Split a string and print the delimiter

By default, the split operator omits the delimiter. However, if you enclose (the delimiter) in small brackets inside double quotes, it will print the delimiter as well. For instance, the following code will split the string wherever the Colon(:) is encountered:

"Name:Designation:JoiningDate:Address" -split "(:)"

The above output shows that the string is separated as well as the colon (:) is also printed as part of the output.

Example 4: Split a string using a specific character

With a split case-sensitive operator, you can also specify a case-sensitive keyword on the basis that the strings/substrings will be split into parts.

The following command will split the strings based on the case sensitivity of a character “s”:

$split -cSplit 's'

You can see in the above that the substrings split on every lowercase ‘s’.

Example 5: Split with a string with Max-substrings

Max-substrings operation is used to return splitting the string into user-defined numbers. When the specified number is reached, the remaining substrings are appended to the last split substrings. For a better understanding, we have divided the process into procedural steps.

First, A string is stored in a variable named “$fruits”:

$fruits = "Apple, Orange, Banana, Peach, Strawberry, Blueberry"

By executing the following command, the $fruits variable splits the string into “3” substrings.

$fruits -split ',', 3

The above output shows that a single line substring breaks into three (3) pieces and the rest of the substrings are appended to the last split.

However, if we put a negative number into the parameter, the string is split in the reverse order.

$fruits -split ',', -3

It can be observed that the substring split into three (3) parts but from the end side.

Example 6: Split a multi-line string

Normally, the split operator splits the string wherever the delimiter is encountered. However, you can use split in multiline statements to break the lines from the end of each line.

For a multiline string, you need to start the string with (@’) and end it with the (‘@) sign. The following code creates and stores a multiline string in a variable named “$topic”.

$topic = @'

>> This is topic 1

>> This is topic 2

>> This is topic 3

>> '
@

Now, run the below-mentioned command to split the multiline string line-by-line.

$topic -split 'multiline'

The output shows that the split operator has split the multiline statements into possible lines.

Congratulations! You have learned to apply the split operator to a variety of scenarios in PowerShell.

Conclusion

The PowerShell split operator is used to split or break the strings/substrings into different parts based on the delimiter. The delimiter could be white space or any character. In this article, you have learned the core concepts of the split operator with practical examples. By default, the split operator splits the string into possible substrings where the whitespace is encountered. However, a user may define the splitting criterion by using a variety of delimiters.

About the author

Adnan Shabbir