Using PowerShell with IndexOf and LastIndexOf to Split Strings

Working with strings is a common task in scripting and programming, and PowerShell provides powerful tools for manipulating and splitting strings. Two useful methods for this purpose are IndexOf and LastIndexOf . In this blog post, we’ll explore using PowerShell with IndexOf and LastIndexOf to split strings.

IndexOf Method

The IndexOf method in PowerShell helps us find the position of a specific character or substring within a given string. Once we know the position of the character or substring, we can split the string accordingly.

$index = $string.IndexOf($substring)

Here, $string is the original string, and $substring is the character or substring we want to find. $index will store the position of the first occurrence of $substring in the string.

Let’s say we have a string containing a sentence, and we want to split it into two parts based on the position of a comma:

$string = "Alkane, Solutions"
$index = $string.IndexOf(",")
$part1 = $string.Substring(0, $index)
$part2 = $string.Substring($index + 1)

In this example, $index will store the position of the comma, and we use Substring to split the string into two parts, resulting in $part1 containing “Alkane” and $part2 containing ” Solutions.”. You will note that there is a preceding ” ” (space) character before “Solutions”, which is the space after the original comma. We can easily remove this using the trim() method like so:

$part2 = $string.Substring($index + 1).trim()

LastIndexOf Method

The LastIndexOf method works similarly to IndexOf , but it finds the position of the last occurrence of a character or substring in the string.

$lastIndex = $string.LastIndexOf($substring)

Here, $string is the original string, and $substring is the character or substring we want to find. $lastIndex will store the position of the last occurrence of $substring in the string.

Suppose we have a file path, and we want to extract the file name from it:

$path = "C:\Alkane\AlkaneSubfolder\alkane.txt"
$lastIndex = $path.LastIndexOf("\")
$fileName = $path.Substring($lastIndex + 1)

In this example, $lastIndex will store the position of the last backslash (“ \ “) in the file path, and $fileName will contain “example.txt.”

PowerShell’s IndexOf and LastIndexOf methods are powerful tools for splitting strings based on the position of characters or substrings. By using these methods, we can extract the data we need from strings, making our scripts and automation tasks more efficient and accurate. These methods are valuable for various use cases, from parsing file paths to processing text data.

Use the PowerShell Substring Method to Extract and Match Part of a String

This post explains how we can use the PowerShell substring method to extract and match part of a string.

Before we continue, it’s important to remember that the first index of a string starts at position 0. So in the following example string of text:

abcdefghijklmnopqrstuvwxyz

The character “a” is at position index 0, “b” is at position index 1 and “z” is at position index 25.

PowerShell Substring Basic Example

Let’s start with the most basic example of extracting part of a string by using the PowerShell substring method with one argument:

substring(int startIndex)

In real-world scenarios, this method is most useful when the string of text we are searching has a consistent pattern. For example, maybe our product database has product references similar to the following:

Reference Item
ALK-3242334UK Laptop
ALK-9876352UK Keyboard
ALK-6622553UK Mouse

And the references are always in the format:

ALK-[7 Digits]UK (“ALK-” followed by 7 numeric digits and finally “UK”).

By providing just the starting index as an argument to the Substring method, we can tell PowerShell to return everything after and including that index. For example, let’s assume we want to strip off the starting “ALK-” part of our reference. We know that:

  • “A” is character index 0
  • “L” is character index 1
  • “K is character index 2
  • “-” is character index 3

So we need everything from character index 4 onwards:

$alkaneString = "ALK-3242334UK"
write-host $alkaneString.Substring(4)

Which will output:

3242334UK

PowerShell Substring Basic Example using String Length

But maybe we don’t want to include the “UK” characters at the end? Luckily the substring method also allows us to specify how many characters we want to return from a particular character index position:

substring(int startIndex, int length)

Since we know that our product references always has 7 digits in the middle, we can specify the start index and length like so:

$alkaneString = "ALK-3242334UK"
write-host $alkaneString.Substring(4,7)

Which will output:

3242334

The plot thickens when we are searching for a string or text in some random output. Consider a requirement to extract product references from this string of random text:

Thank you for purchasing the Alkane Laptop (ALK-3242334UK), Keyboard (ALK-9876352UK) and Mouse (ALK-6622553UK).  We value your custom and look forward to seeing you in future.

We have no idea which point in the sentence our product references will appear. In this example, the only way we can extract our product references is by using the select-string cmdlet with a regular expression.