Recently, I got a requirement to check the number of characters in a string in PowerShell. PowerShell provides different methods to do so. In this tutorial, I will show you how to count the number of characters in a string in PowerShell using various methods.
To count the number of characters in a string in PowerShell, you can use the .Length property, which is the simplest and most direct method. For example, $string = “Hello, PowerShell!”; $charCount = $string.Length will store the character count in $charCount.
Method 1: Using the .Length Property
To count the number of characters in a string in PowerShell, you can use the .Length property. This property is available on all string objects in PowerShell and returns the total number of characters in the string.
Here is a complete example.
# Define a string
$string = "Hello, PowerShell!"
# Count the number of characters using the .Length property
$charCount = $string.Length
# Output the result
Write-Output "The number of characters in the string is: $charCount"In this example, the output will be:
The number of characters in the string is: 18You can see the output in the screenshot below after I executed the script using VS code.

Read Case Insensitive Strings Comparison in PowerShell
Method 2: Counting Specific Characters with Where-Object
If you need to count the occurrences of a specific character within a string, you can use the Where-Object cmdlet in combination with the ToCharArray() method to filter and count specific characters.
Here is a complete PowerShell script.
# Define a string
$string = "Hello, PowerShell!"
# Define the character to count
$charToCount = "l"
# Convert the string to a character array and count the occurrences of the specific character
$charCount = ($string.ToCharArray() | Where-Object { $_ -eq $charToCount }).Count
# Output the result
Write-Output "The number of occurrences of '$charToCount' in the string is: $charCount"In this example, the output will be:
The number of occurrences of 'l' in the string is: 3This method is useful when you need to count how many times a specific character appears in a string.
Method 3: Using Regular Expressions with Select-String
In PowerShell, you can also use regular expressions to get the number of characters in a string. The Select-String cmdlet, combined with regular expressions, we can use for the same purpose.
Here is a complete example.
# Define a string
$string = "Hello, PowerShell!"
# Define the regular expression pattern to match the character
$pattern = "l"
# Use Select-String to find matches and count them
$charCount = ($string | Select-String -Pattern $pattern -AllMatches).Matches.Count
# Output the result
Write-Output "The number of occurrences of '$pattern' in the string is: $charCount"In this example, the output will be:
The number of occurrences of 'l' in the string is: 3You can see the output in the screenshot below:

Read Add Double Quotes in a String in PowerShell
Method 4: Using Measure-Object
Let us check another method for this.
The Measure-Object cmdlet in PowerShell can also count characters in a string. This method is particularly useful when dealing with multiple strings or when counting characters across multiple lines.
Here is an example.
# Define a string
$string = "Hello, PowerShell!"
# Convert the string to a character array and measure the length
$charCount = ($string.ToCharArray() | Measure-Object).Count
# Output the result
Write-Output "The number of characters in the string is: $charCount"In this example, the output will be:
The number of characters in the string is: 18You can see the output in the screenshot; it gave me the exact output.

Conclusion
In PowerShell, the easiest way to count the number of characters in a string is by using the .Length property. However, you can also use other methods like Measure-Object and Regular Expressions with Select-String to get the number of characters in a string in PowerShell.
I hope the above examples help you understand. If you still have any questions, leave a comment below.
You may also like:
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.