Get Length of String in PowerShell

Using String’s Length property

Use string’s length property to get length of String in PowerShell e.g. "Hello World".Length. System.String‘s Length property returns number of Characters present in the String.

Output

Here, we declared a string variable blog_Name which contains string Java2blog.

We have used Length property on the String variable to get length of String in PowerShell.

Length property returns number of characters present in the String.

You can directly use Length property even without creating variable.

Output

You can use Length property with if statement to do comparison based on length of String.

For example:
Let’s say you want to check if length of String is greater than 8 or not.

You can use following code:

Output

We used -gt operator to check if String is greater than 8 or not.

Using Measure-Object with Character parameter

Use Measure-Object cmdlet with Character parameter to get length of String in PowerShell.

Output

Here, we declared a string variable blogName which contains string "Java2blog" and piped it with Measure-Object.

Measure-Object cmdlet provides the numeric properties of objects, and the characters, words, and lines in string objects, such as files of text.

We passed -Character as parameter to Measure-Object cmdlet to get total number of characters present in String.

You can also pass -word as parameter in case you want to count total number of words in String.

Output

That’s all about how to get length of String in PowerShell.

Was this post helpful?

Related Posts

  • 28 January

    PowerShell – Get Number of Lines in CSV File

    Table of ContentsUsing Get-Content CmdletUsing Import-Csv CmdletUsing Get-ChildItem with Foreach-Object Using Get-Content Cmdlet The Get-Content cmdlet can be used in the following ways to get the number of lines in the specified .csv file: Get-Content cmdlet with .Length property Get-Content cmdlet with .Count property Get-Content cmdlet with .ReadCount property Get-Content cmdlet with Measure-Object cmdlet and […]

  • Read CSV file in PowerShell
    28 January

    Read CSV File in PowerShell

    Table of ContentsUsing the Import-Csv CmdletUsing the ConvertFrom-Csv CmdletUse the ConvertFrom-Csv Cmdlet with the Pipe (|) OperatorUse the ConvertFrom-Csv Cmdlet with the -InputObject Parameter Using the Import-Csv Cmdlet To read a CSV file in PowerShell, use the Import-Csv cmdlet. [crayon-63da636580176306954004/] [crayon-63da63658017d053802949/] PowerShell provides several cmdlets that allow us to work with CSV files: Import-Csv ConvertFrom-Csv […]

  • Read File into String in PowerShell
    27 January

    Read File into String in PowerShell

    Table of ContentsUsing the Get-Content CmdletUse the Get-Content Cmdlet to Read a Single FileUse the Get-Content Cmdlet with forreach loop to Read Multiple FilesUsing [System.IO.File]::ReadAllText() MethodUse the [System.IO.File]::ReadAllText() Method with foreach loop to Read Multiple FilesUsing the Variable Notation with the Out-String Cmdlet Using the Get-Content Cmdlet We can use the Get-Content cmdlet to read […]

  • Remove property from Object in PowerShell
    27 January

    Remove Property from Object in PowerShell

    Table of ContentsUsing PSObject.Properties.Remove() MethodUsing -ExcludeProperty Parameter Using PSObject.Properties.Remove() Method To delete a property from an object in PowerShell, Use the New-Object cmdlet to create an object. Use the PSObject.Properties.Remove() method to remove a specific property from an object by providing the property’s name as a string parameter. [crayon-63da63658031a677690461/] [crayon-63da63658031e334792710/] In the above code, we […]

  • Create File if not exists PowerShell
    26 January

    Create File If Not Exists in PowerShell

    Table of ContentsUsing the if-else StatementUse the New-Item CmdletUse the Out-File CmdletUsing the try-catch StatementUse the New-Item CmdletUse the Out-File CmdletUsing One-Liners Using the if-else Statement We can use the if-else statement to create a file as follows: Use the New-Item Cmdlet To create a file if it does not exist in PowerShell: Use the […]

  • Check if folder exists in PowerShell
    26 January

    Check if Folder Exists in PowerShell

    Table of ContentsUsing Built-in Cmdlets and MethodsUse the Test-Path CmdletUse the [System.IO.Directory]::Exists() MethodUse the Get-Item CmdletOther CmdletsCreating Custom Alias Using Built-in Cmdlets and Methods We can use built-in cmdlets and methods to check if a directory exists. Use the Test-Path Cmdlet To check if a folder exists, use the Test-Path cmdlet. [crayon-63da63658061a596077764/] [crayon-63da63658061e609064933/] PowerShell’s Test-Path […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.