How to Count Occurrences of a Substring in a String in PowerShell?

Recently, I was working on a PowerShell script to get the count of occurrences of a substring in a string. I am going to explain in detail, with examples here, how to count occurrences of a substring in a string in PowerShell.

To count occurrences of a substring in a string in PowerShell, you can use several methods. The -split operator splits the string by the substring and counts the resulting parts minus one, e.g., ($string -split $substring).Count - 1.

There are various methods to do so. Let me explain to you one by one.

1- Using the -split Operator

The PowerShell -split operator is an efficient way to count occurrences of a substring. When you split a string by the substring, the number of parts minus one gives the count of the substring.

Here is an example.

$string = "John, Mary, John, Michael, John"
$substring = "John"
$count = ($string -split $substring).Count - 1
Write-Output $count

In this example, the string is split by the substring “John”. The resulting array has four parts, which means “John” occurs three times.

I executed the above PowerShell script using VS code, and you can see the output in the screenshot below:

Count Occurrences of a Substring in a String in PowerShell

Check out Check if a String Contains a Substring in PowerShell

2- Using the Select-String Cmdlet

You can also use the Select-String PowerShell cmdlet to count occurrences of a substring in a string.

Select-String cmdlet search for text using regular expressions. It can also count the number of matches in a string in PowerShell.

Let me show you an example.

$string = "John, Mary, John, Michael, John"
$substring = "John"
$count = ($string | Select-String -Pattern $substring).Matches.Count
Write-Output $count

This method uses Select-String to find all matches of the substring “John” and counts them.

You can see the output in the screenshot below:

How to Count Occurrences of a Substring in a String in PowerShell

3- Using the -replace Operator

The -replace operator in PowerShell can also be used to count occurrences by replacing the substring with an empty string and comparing lengths.

Here is an example.

$string = "John, Mary, John, Michael, John"
$substring = "John"
$count = ($string.Length - ($string -replace $substring, "").Length) / $substring.Length
Write-Output $count

Here, the length of the string without the substring is subtracted from the original length, and the result is divided by the length of the substring to get the count.

You can have a look at the below screenshot for the exact output.

PowerShell Count Occurrences of a Substring in a String

Read Check if a String Contains Multiple Values in PowerShell

4- Using the .IndexOf Method in a Loop

The .IndexOf method can be used in a loop to count occurrences in a string in PowerShell.

Here is a complete example.

$string = "John, Mary, John, Michael, John"
$substring = "John"
$count = 0
$index = 0

while ($index -ne -1) {
    $index = $string.IndexOf($substring, $index)
    if ($index -ne -1) {
        $count++
        $index += $substring.Length
    }
}

Write-Output $count

This method iterates through the string, finding each occurrence of the substring and counting them.

You can check the exact output in the screenshot below:

PowerShell how to Count Occurrences of a Substring in a String

5- Using Regular Expressions

Regular expressions in PowerShell can also be used to search for patterns in strings. PowerShell supports regular expressions through the System.Text.RegularExpressions.Regex class.

Here is an example.

$string = "John, Mary, John, Michael, John"
$substring = "John"
$regex = [regex]::Escape($substring)
$count = [regex]::Matches($string, $regex).Count
Write-Output $count

In this example, the substring is escaped to ensure it is treated as a literal string, and Matches method is used to count occurrences.

Conclusion

PowerShell offers multiple methods to count occurrences of a substring within a string. I have explained each method with examples of counting occurrences of a substring in a string in PowerShell. I hope you find this tutorial helpful.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.