
A string is a sequence of characters that represents text. In PowerShell, strings are enclosed in quotation marks and can be assigned to variables. String length refers to the number of characters in a string, including white spaces, punctuation marks, and special characters.
As a PowerShell scripter, knowing the string length is useful when validating input, trimming, or splitting strings. PowerShell provides various methods for measuring the length of a string object, and in this article, we will explore these techniques in detail.
Different methods to get the length of a string in PowerShell
PowerShell provides several ways to measure the length of a string, including using the Length property and Measure-Object cmdlet. Let’s explore each of these methods in detail.
Using the Length property to find the length of a string
The simplest way to get the length of a string in PowerShell is to use the Length property. This property returns the number of characters in a string, including spaces and special characters. Here is an example script:
$String = "This is my string" $length = $String.Length $length #Output: 17
As you can see, the Length property returns the length of the string in characters. You can also directly get the length of a string literal like this:
"This is my string".Length
Strings may be defined using double quotes or single quotes in PowerShell scripting. You can compare string lengths with “IF” statements. For example, to check if a string has more than 8 characters:
$String = "Hello, World!"
If ($String.Length -gt 8) { Write-Output "The string has more than 8 characters."
}Get string length in PowerShell using Measure-Object cmdlet
$String = "This is my string" $length = $String | Measure-Object -Character $length.Characters
As you can see, the Measure-Object cmdlet returns the length of the string in characters. Here is the output in the PowerShell console:

Calculating the length of a string in a pipeline
$Strings = @("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
$Strings | ForEach-Object { $_.Length }Here, ‘$strings’ is an array of strings, and the command inside ForEach-Object calculates the length of each element and returns it.
Regular Expression to Check if a string is at least in a specific Length
You can use the RegEx -Match operator to test if the given string is at least a specific length. E.g., let’s check if a variable is at least 8 characters long.
$UserName = Read-Host "Enter the User Name"
If ($UserName -match ".{8}" )
{ Write-Output "User Name is atleast 8 characters long!"
}Wrapping up
In this article, we explored the basics of measuring PowerShell string length with different methods and various scenarios. The length property provides a quick way to get the length of any string variable or literal. For pipeline input, Measure-Object -Character gives you the count along with other stats.
- Using the
.Lengthproperty to get the length of a string variable. - Passing strings to
Measure-Object -Characterto get the length.
Knowing how to get the string length unlocks many text processing capabilities and is a useful tool to have in your PowerShell skills. With this comprehensive overview, you should have all the knowledge to find, validate and work with string lengths in PowerShell.
This particular example will return the length of the string stored in the variable named $my_string.
This particular example will check if the length of the string stored in the variable named $my_string is greater than 10 and return a specific string based on the result.
= ""

We can see that the string stored in the variable named $my_string has a length of 15.
Note that this length includes all alphabetical characters in the string along with the space and the exclamation point.
Example 2: Check if String Length is Greater than Value in PowerShell
= ""

The if else statement returns String Length greater than 10 since the length of the string (15) is indeed greater than 10.
- lt: less than
- le: less than or equal to
- gt: greater than
- ge: greater than or equal to
- ne: not equal
Feel free to use whichever operator you would like.
PowerShell: How to Replace Text in String
PowerShell: How to Extract Text Between Two Strings
PowerShell: How to Check if String Exists in List of Strings
PowerShell: How to Replace Every Occurrence of String in File

