Powershell has multiple ways to get the length of a string. You could find the length of a static string or you could use a variable and store different length strings in the variable for finding the length.
Powershell has in-built cmdlets that can easily give you the length of a string. We will look at the ways by which you can get the length of a string.
Find the length of a string using the length property in Powershell
The simplest way to find the length of a string is by placing the string in a variable and using the length property to find the length of the string.
In the examples below, we will use two cases. In the first example, we place a single word string and assign it to a variable. We use the length property to find the length of the single word. Here is the command for the first example:
$str1=”TECHEPAGES”
$str1.length
In the second example, we will assign a sentence to the string variable and measure its length using the length property. The commands in this example are shared hereunder:
$str1=”TECHEPAGES is a technical website and publishes useful tech tutorials”
$str1.length
The output of both examples is shared in the screenshot below.

If you are looking for a direct method to find the string length quickly for a specific word or sentence, you could use another approach as discussed below.
“TECHEPAGES”.length
“TECHEPAGES is a technical website and publishes useful tech tutorials”.length
The output of these commands will also display the length of the strings on the display console. The screenshot below displays the command output below.

Find the length of a string using the Measure-Object cmdlet in Powershell
Measure-object cmdlet goes over and beyond the length property. It helps in measuring string length in terms of the number of characters or words or both. You could define a string in a variable. Or, you could use a static string to find the number of words or characters in it.
We discuss the Measure-Object cmdlet and its application below with examples.
In the first example, we will find the number of words and characters in a string that has been assigned to a variable.
$str2=”TECHEPAGES is a technical website and publishes useful tech tutorials”
$str2 | Measure-Object -word -character
These two commands define a string and use the Measure-Object cmdlet to find the number of words and characters in the string.
An alternative approach is to use the string directly alongside the Measure-Object cmdlet as displayed below.
“TECHEPAGES is a technical website and publishes useful tech tutorials” | Measure-object -word -character
In both cases, the output of the command is similar. Please see the screenshot below. It shows both examples and the output of the command.

You can also see that there is an option to find the number of lines in a string. This could be very useful if you were to find the number of lines in a text file.
So, we take another example wherein we place a string of multiple lines in a variable and use the Measure-Object cmdlet to find the length of the string.
$str3=”TECHEPAGES is a technical website and publishes useful tech tutorials.
You could share feedback with us. Or, you could even get us to write on a topic of your choice.
This could include topics about Windows Updates or Microsoft Azure Cloud platform”
$str3 | Measure-object -line -word -character
The output of this command will find the number of lines, words, and characters in the string. We can see from the screenshot below that we were able to get the number of lines, words, and characters in a string that was defined through a variable.

Summary
In this Powershell tutorial, we have discussed two different ways by which we can find the length of a string. I prefer using the Measure-Object because of the possibilities that we get from the cmdlet in Powershell.
But, if you are looking for a quick way to work through simpler requirements, you could just use the length property method for finding the length of a string.
Suggested Powershell Tutorials
The following Powershell tutorials can be used to perform various system administration tasks on a Windows computer.
- How to find disk block size in Powershell?
- How to list installed printers in Powershell?
- How to find current directory path in Powershell?
- How do I convert epoch time to date in Powershell?
- How to find the public IP address using Powershell?
- How to find the boot directory using Powershell?
- How to find DoH DNS Servers using Powershell?
- How to get sid from the user account in Powershell?
- How to get system boot time in Powershell?
- How to append to a file in Powershell?
Rajesh Dhawan is a technology professional who loves to blog about smart wearables, Cloud computing and Microsoft technologies. He loves to break complex problems into manageable chunks of meaningful information.