Recently, one of my team members asked me to find the length of a string using PowerShell. I tried different methods for this. In this tutorial, I will show you how to get length of a string in PowerShell using various methods.
To get the length of a string in PowerShell, you can use the .Length property, which directly returns the number of characters in the string. For example, $string = “Hello, PowerShell!”; $length = $string.Length will set $length to 17. You can also use the Measure-Object cmdlet: $length = ($string | Measure-Object -Character).Characters. Both methods effectively provide the string’s length.
Here are the methods with examples.
Method 1: Using the .Length Property
In PowerShell, you can use the .Length property of the string object to get the length of a string in PowerShell. This property returns the number of characters in the string.
Here is a simple example.
# Define a string variable
$string = "Hello, Washington!"
# Get the length of the string
$length = $string.Length
# Output the length
Write-Output "The length of the string is: $length"In this example, the output will be:
The length of the string is: 17Once you execute the PowerShell script, you can see the output in the below screenshot.

Read Get String Length in Bytes in PowerShell
Method 2: Using the Measure-Object Cmdlet
Another way to find the length of a string in PowerShell is to use the Measure-Object cmdlet. This cmdlet can measure the properties of objects, including the length of a string.
Here is another example.
# Define a string variable
$string = "Hello, New York!"
# Use Measure-Object to get the length
$length = ($string | Measure-Object -Character).Characters
# Output the length
Write-Output "The length of the string is: $length"In this example, the Measure-Object cmdlet is used to measure the number of characters in the string.
I executed the above code using VS code; you can see the output in the screenshot below.

Method 3: Using a Custom Function
You can also write a custom function to get the length of a string in PowerShell. That function you can reuse it.
Here is the complete PowerShell script.
# Define a custom function to get string length
function Get-StringLength {
param (
[string]$inputString
)
return $inputString.Length
}
# Define a string variable
$string = "Hello, PowerShell!"
# Call the custom function
$length = Get-StringLength -inputString $string
# Output the length
Write-Output "The length of the string is: $length"You can also see the output after I executed the script in the screenshot below.

Read How to Compare Strings in PowerShell?
Method 4: Using Regular Expressions
In PowerShell, you can also use regular expressions to find the length of a string.
Here is a simple example:
# Define a string variable
$string = "Hello, PowerShell!"
# Use regular expressions to get the length
$length = ([regex]::Matches($string, ".")).Count
# Output the length
Write-Output "The length of the string is: $length"In this example, the regex class is used to match each character in the string in PowerShell, and the count of matches gives the length of the string.
PowerShell String Length Greater Than
In PowerShell, you can check if a string’s length is greater than a certain value using the -gt comparison operator, along with the .Length property of the string. Here’s an example:
$dbUserName = "JohnDoe123"
if ($dbUserName.Length -gt 8) {
Write-Output "The username is longer than 8 characters."
} else {
Write-Output "The username is 8 characters or shorter."
}In this example, we define a string variable $dbUserName with the value “JohnDoe123”. We then use an if statement to compare the length of the string using $dbUserName.Length with the value 8 using the -gt (greater than) operator.
If the length of the string is greater than 8, the condition evaluates to $true, and the code inside the if block is executed, outputting “The username is longer than 8 characters.”
Otherwise, if the length is 8 or less, the else block is executed, outputting “The username is 8 characters or shorter.”
I executed the above PowerShell script, and you can see the output in the screenshot below:

In this tutorial, I have explained how to get the length of a string in PowerShell using the .length property. Also, I have explained how to use the Measure-Object cmdlet to find the length of a string in PowerShell.
If you still have questions, then give me a comment below.
You may also like:
- Convert Variables to Strings in PowerShell
- How to Filter Strings in PowerShell?
- How to Use PowerShell to Encode and Decode Strings?
- Get the First and Last Line of a Multiline String in 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.