Powershell

How to Split Strings in PowerShell

In programming, a string is a sequence of characters treated as a single piece of data. A string will store textual information. It’s a part of fundamental programming principles.

PowerShell is a shell and a scripting language. It’s like a programming language itself. The PowerShell scripting supports all the major principles of programming, including strings. The primary difference is that shell scripts are generally designed to automate tasks without building a dedicated app.

In this guide, we’ll work with strings in PowerShell. It will also demonstrate how to split a string and use the techniques in various situations.

Strings in PowerShell

First, let’s have a quick look at a sample PowerShell string. From the PowerShell shell, run the following command.

$ $HelloWorld = "The quick brown fox jumps over the lazy dog"

It creates a temporary variable “HelloWorld” that stores the string “The quick brown fox jumps over the lazy dog”. Calling the variable will return its value.

$ $HelloWorld

Arrays in PowerShell

In programming, an array is a sequential collection of the same type of elements. It can be of any data type. Working with an array is significantly simple. You can traverse an array using for loop or while loop. The size of an array is known, making the task significantly easier.

This section will act just as a preliminary introduction to arrays. If you have a background in programming in any other language, then this should be easier to understand. In the following example, we’re going to create an array of strings.

$ $HelloArray = "The", "quick", "brown", "fox"

Calling the variable “HelloArray” will print all the elements of the array.

$ $HelloArray

You can call an individual element of the array by its position in the sequence.

$ $HelloArray[0]

Note that the position counting starts at 0. So,

  • Element 1 -> position value 0.
  • Element 2 -> position value 1.
  • Element 3 -> position value 2.

And so on.

Splitting string

Splitting a string into a character array

Any string variable will have a default function .ToCharArray() that converts the string into an array of Unicode characters. It’s a part of the .NET framework feature. PowerShell allows using various .NET features in the scripts.

Let’s convert HelloWorld into a character array.

$ $HelloWorld = "The quick brown fox"
$ $HelloWorldArray = $HelloWorld.ToCharArray()

Let’s see the result.

$ $HelloWorldArray

We can check the individual elements of the array.

$ $HelloWorldArray[0]
$ $HelloWorldArray[4]

Splitting string based on delimiter

Any string variable comes with a handful of built-in functions. With the help of the function .Split(), we can split a string based on a certain delimiting character. It splits a string into an array of sub-strings. By default, it uses the whitespace character as the delimiter. However, you can also provide a custom delimiting character.

Let’s check it out in action. First, declare our string variable.

$ $HelloWorld = "The quick brown fox jumps over the lazy dog"

Now, call the .Split() function and store the resultant in a second variable.

$ $HelloWorldSplit = $HelloWorld.Split()

Check the result of the split.

$ $HelloWorldSplit

To specify split using a specific character, you have to pass the value as the function parameter. In the next example, we’ll split the string based on comma (,).

$ $HelloWorld = "The,quick,brown,fox"
$ $HelloWorldArray = $HelloWorld.Split(",")
$ $HelloWorldArray

Another interesting feature is the support for regex (regular expression). You can pass regular expressions as the parameter for the .Split() function and have finer control over the result.

Splitting numbers and alphabets

In many cases, a string will consist of both alphabets and digits. With the help of the flag “-Split”, we can separate all the characters and digits into individual arrays.

Let’s check it out in action. Here, we have an alphanumeric string.

$ $HelloWorld = "r1a2n3do0m6"

To get only the alphabets, we’ll use the flag “-Split”. As for the argument, “\d” tells to remove all the digits.

$ $HelloWorldArray = $HelloWorld -Split "\d"
$ $HelloWorldArray

To get only the digits, use the flag “-Split” with the argument “\D”.

$ $HelloWorldArray = $HelloWorld -Split "\D"
$ $HelloWorldArray

Miscellaneous

Trimming strings

Various strings may contain excess characters at the start or the end. We can remove them using the trim function.

$ " the quick brown fox ".Trim()

You can also specify which characters to trim.

$ "111000111".Trim("1")

Using the .Trim() function will remove excess characters both at the start and at the end. To trim only a specific side, we can use .TrimStart() or .TrimEnd(). They work just as the label suggests.

$ "111000111".TrimStart("1")

$ "111000111".TrimEnd("1")

Final thoughts

This article showcases how to split strings in PowerShell. It incorporates two programming principles – array and string. Thanks to .NET support, there are various features available to manipulate a string. It covers how to convert a string into arrays of sub-strings or characters.

This is just the tip of the iceberg. PowerShell supports numerous more ways to interact and use strings. Check out these guides on substrings, array of strings, concatenating strings, etc.

Happy computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.