Find the Position of Substring in PowerShell

Rohan Timalsina Feb 27, 2022
  1. String and Substring in PowerShell
  2. Use the IndexOf Method to Find the Position of Substring in PowerShell
  3. Use the LastIndexOf Method to Find the Position of Substring in PowerShell
  4. Use the Select-String Cmdlet to Find the Position of Substring in PowerShell
Find the Position of Substring in PowerShell

This tutorial will introduce different methods to find the position of a substring in PowerShell.

String and Substring in PowerShell

A string is the common data type used in PowerShell. It is the sequence of characters to represent texts.

You can define a string in PowerShell by using single or double-quotes. For example, “ABC” or ‘ABC’.

A substring is any specific text inside a string. If ABC is a string, B is its substring.

Sometimes, there might be a condition when you need to find a certain part of a string or the position of a substring in a text file. The IndexOf method and LastIndexOf mainly report the index position of a specified substring in PowerShell.

Use the IndexOf Method to Find the Position of Substring in PowerShell

The IndexOf method allows you to find the first occurrence of the specified string in the string object.

In this method, you can specify the string, the starting search position in the string object, the number of characters to search, and the type of rules to use for the search.

The example below finds the position of the first occurrence of a substring to in the given string.

"Welcome to PowerShell Tutorial".IndexOf("to")

Output:

8

The IndexOf method counts the index from zero, which prints the output 8.

In the following code, 9 is specified as the starting search position in the string.

"Welcome to PowerShell Tutorial".IndexOf("to", 9)

When you specify the starting search position, the character before that position is ignored, so it finds the position of the first occurrence of a substring to after index 9.

Output:

24

If the specified string does not match, it returns the value -1.

For example, searching for a substring py in the given string prints the result -1.

"Welcome to PowerShell Tutorial".IndexOf("py")

Output:

-1

Use the LastIndexOf Method to Find the Position of Substring in PowerShell

You can also use the LastIndexOf method to find the index of a substring in PowerShell. The LastIndexOf method finds the position of the last occurrence of a certain character inside a string.

The following command finds the index of the last occurrence of a substring to in the given string.

"Welcome to PowerShell Tutorial".LastIndexOf("to")

As shown, it prints the position of the last appearance of a substring to.

Output:

24

Let’s see another example with a string variable $hello.

$hello="HelloHelloHello"

If you use the LastIndexOf method to find the substring He in $hello, it returns 10 because it is the last index where He occurs.

$hello.LastIndexOf("He")

Output:

10

If the substring does not match, it also returns -1.

For example:

$hello.LastIndexOf("Hi")

Output:

-1

Use the Select-String Cmdlet to Find the Position of Substring in PowerShell

The Select-String cmdlet searches for texts in strings and files. It is similar to grep in UNIX.

By default, it finds the first match in each line and displays all text containing the match.

$hello | Select-String "He"

Output:

HelloHelloHello

The Matches property of Select-String finds the match between the specified string and the input string or file. The position of the matching substring is stored in the Index property.

The following example shows how you can use the Matches.Index property to find the position of a substring inside a string.

("HelloHelloHello" | Select-String "He").Matches.Index

It prints only the index 0 because Select-String stops after the first match in each line of text.

Output:

0

You can use the -AllMatches parameter to search for more than one match in each line.

("HelloHelloHello" | Select-String "He" -AllMatches).Matches.Index

As shown, it prints all the positions of the specified substring.

Output:

0
5
10

The following example finds all the positions of a specified substring in a text file test.txt.

(Get-Content test.txt | Select-String "the").Matches.Index

Output:

10
47
17

This method is the best option to find all the characters’ positions in a string.

We hope this article helps you understand finding a substring’s index position in PowerShell.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell String