PowerTip: Get First 140 Characters from String with PowerShell


Summary: Use Windows PowerShell to retrieve the first 140 characters from a string.

Hey, Scripting Guy! Question How can I use Windows PowerShell to retrieve the first 140 characters from a string?

Hey, Scripting Guy! Answer Use the SubString method from a string. Specify the starting point (in this case 0) and the
           number of characters to retrieve (in this case 140). The following example assumes that $a 
           contains a string with more than 140 characters:

$a.SubString(0,140)

Comments (2)

  1. hpotsirhc says:

    If the string length is unknown and not neccessarily at least 140, you can use the cool trick:
    ($a.ToCharArray | select -first 140) -join ""

  2. Ed Wilson says:

    @Hpotsirhc that is cool.

Skip to main content