How do I find the length of string in Powershell?

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.

String length in Powershell using length property.

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.

Length of a string using Powershelll

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.

Find the length of a string with multiple lines using the Measure-Object command in Powershell.

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 useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Rajesh Dhawan

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.