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 $countIn 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:

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 $countThis method uses Select-String to find all matches of the substring “John” and counts them.
You can see the output in the screenshot below:

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 $countHere, 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.

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 $countThis method iterates through the string, finding each occurrence of the substring and counting them.
You can check the exact output in the screenshot below:

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 $countIn 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:
- Check if a String Contains Special Characters in PowerShell
- Extract Text from Strings Between Delimiters Using PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.