Home » PowerShell » String Length of Variable in PowerShell

String Length of Variable in PowerShell

Use the $var.Length to get the string length of a variable in PowerShell. PowerShell string has System.String Object type. The Length property on the string variable gets the count of the number of characters in a string.

The PowerShell string contains a sequence of characters, Length property returns the number of characters or length and not the number of Unicode characters.

Measure-Object -Character in PowerShell count characters in a string. The Measure-Object cmdlet uses the Character parameter to check the length of a string.

In this article, we will discuss how to use $string.Length property to get the length of a variable in PowerShell and count the number of characters in a string.

Get the String Length of Variable in PowerShell

Use the System.String type Length property to get the length of a string variable in PowerShell.

In the following example, use the Length property to check the string length of a variable $str.

$str = "PowerShell"
# Use Length property
$str.Length       

In the above PowerShell script, the $str variable contains the string PowerShell. We have used the Length property over the $str variable, it returns the string length or the number of characters in a string.

The output of the above script in PowerShell counts characters in a string.

Get String Length of Variable in PowerShell
Get String Length of Variable in PowerShell

In PowerShell, we can define a string in double quotes or single quotes. To get the string length, we can use the Length property over the string.

"ShellGeek".Length  

The above PowerShell script uses the Length property of the string and returns the string length as given below

PS C:\> "ShellGeek".Length                                                                                              
9
PS C:\>                                                                                                                              

Get String Length of a Variable using Measure-Object

Measure-Object cmdlet in PowerShell calculates the numeric property values of objects and characters, and words in a string. It counts the number of characters in a string.

Use the Measure-Object to get the string length of a variable in PowerShell. It has a Character parameter that counts the number of characters in the string.

$str = "ShellGeek"
$str | Measure-Object -Character     

In the above PowerShell script, the $str variable contains the string data. it pipes the string data to the Measure-Object command.

The Measure-Object uses the Character parameter to count the number of characters in the string object and returns the string length of a variable in PowerShell.

The output of the above script in PowerShell to check the string length of a variable is:

PS C:\> $str = "ShellGeek"                                                                                              
PS C:\> $str | Measure-Object -Character           
                                                                     
Lines Words Characters Property
----- ----- ---------- --------
                     9

It returns the “ShellGeek” string size in PowerShell as 9.

Conclusion

I hope the above article helped you to understand how to get the string length of a variable in PowerShell using the Length property as well as the Measure-Object command to count the number of characters in a string.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Recommended Content

PowerShell Substring – How to extract the substring in PowerShell

PowerShell Substring IndexOf – Find the position of the substring.

PowerShell Substring After Character – Extracts the Substring after a character.

PowerShell String Replace – How to replace a string in PowerShell.

PowerShell Concatenate String