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 number of characters in a string.

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

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

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 a number of characters in a string.

The output of the above PowerShell script is:

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  

In the above PowerShell script, we have the Length property over the string itself, it 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 the characters, words 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 to check the string length of a variable is:

PS C:\> $str = "ShellGeek"                                                                                              
PS C:\> $str | Measure-Object -Character           
                                                                     
Lines Words Characters Property
----- ----- ---------- --------
                     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.

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

Leave a Comment