You can use the following syntax in PowerShell to count the number of occurrences of a specific character in a string:
([regex]::Matches($my_string, "e" )).count
This particular example will count the number of times that the character “e” occurs in the string variable $my_string.
The following example shows how to use this syntax in practice.
Example: How to Count Specific Characters in String Using PowerShell
Suppose that we would like to count the number of occurrences of the character “e” in the following string:
- “hello everyone, how are you all today?”
We can use the following syntax to do so:
$my_string = "hello everyone, how are you all today?" ([regex]::Matches($my_string, "e" )).count
The following screenshot shows how to use this syntax in practice:

From the output we can see that the character “e” occurs 5 times in the string.
We can manually verify that this is correct by identifying each “e” in the string:
- “hello everyone, how are you all today?”
There are indeed 5 occurrences of the character “e” in the string.
Note that we can use similar syntax to count the number of times a particular substring occurs in a string.
For example, we can use the following syntax to count the number of times the substring “how” occurs in the string:
$my_string = "hello everyone, how are you all today?" ([regex]::Matches($my_string, "how" )).count
The following screenshot shows how to use this syntax in practice:

From the output we can see that the substring “how” occurs 1 time in the string.
Related Tutorials
The following tutorials explain how to perform other common tasks in PowerShell:
PowerShell: How to Find Position of Character in String
PowerShell: How to Replace Every Occurrence of String in File
PowerShell: How to Replace Text with Wildcard
PowerShell: How to Replace Special Characters in String
