Find the Position of a Substring in PowerShell

This article discusses ways of finding a substring’s position in a PowerShell string. We will, in particular, discuss the following three methods, each serving a unique purpose.

  • Method 1: Using String.IndexOf() method,
  • Method 2: Using String.LastIndexOf(), and
  • Method 3: Using Select-String and regular expressions

Method 1: Locating a Substring with String.IndexOf()

The String.IndexOf() method returns the index of the first occurrence of the substring. The method has the following syntax:

String.IndexOf(str, start_value, string_comparison)

Where str is the substring we are looking for in the String, start_value specifies the starting position for the current search, and string_comparison specifies the culture, case, and sort rules applied to a given object.

Note: PowerShell uses zero-based indexing – indexing starts with 0. The following Figure shows the PowerShell indexing example

Let’s now see some examples.

The first search started from the start (index 0 – the default behavior), finding the first occurrence of “so”. The second example starts the search from index 3; therefore, the second occurrence of “so” is located, but the first occurrence is missed.

If the specified substring is not found, as shown in the last search, String.IndexOf() method returns -1.

It is also important to note that String.IndexOf() method is case-sensitive by default. The following example shows that the method does not find the first occurrence of the word “So” because the first letter is capitalized.

You can overcome that behavior by supplying the StringComparison Enum with the value CurrentCultureIgnoreCase, as shown below.

You can also find indexes for which the substring we are looking for is spanning. For example, the substring “so good” in the “so far, so good” String starts from the index 8 to 15, as shown below.

So far, we have discussed how to find the first occurrence of the substring. But what if we have multiple occurrences of the substring or want to find the last occurrence? Let’s answer those questions in methods 2 and 3.

Method 2: Using String.LastIndexOf()

This method works like String.IndexOf() is discussed in Method 1, except that it returns the last occurrence of the substring. The general syntax of the method is as follows.

String.LastIndexOf(str, start_position, string_comparison)

All the parameters match those discussed in the String.IndexOf() but start_position specifies the search should start and move backward.

Note: This method does not reverse indexing but reverses the search.

Let’s see an example.

$str.LastIndexOf(“so”) means we want to search the whole string in a backward manner and $str.LastIndexOf(“so”, 3) searches the first four characters of the string. The following Figure should make the concept clearer.

Method 3: Using Select-String and Regular Expressions

The first two methods have one clear weakness – they do not find multiple occurrences. In this third method, we want to solve that using the Select-String cmdlet.

The cmdlet has the following syntax (showing just a few parameters):

Select String [-AllMatches -CaseSensitive]

All the parameters shown above are optional. When -the AllMatches parameter is passed, all matches for a given pattern are returned, and -CaseSensitive makes the search case-sensitive (by default, matches aren’t case-sensitive). You can read more about Select-String from the documentation.

Select-String cmdlet uses regex to match patterns in strings and files. It is similar to grep in UNIX and findstr.exe in Windows.


This guide discussed three methods for searching for a substring in a String using PowerShell. The first method showed how to get the first occurrence, the second method discussed finding the last occurrence, and the last method explained how to use the Select-String cmdlet and regular expressions to match more complicated substring.