PowerShell: How to Trim First N Characters from String


Often you may want to remove the first N characters from a string in PowerShell.

You can use the following methods to do so:

Method 1: Remove First N Characters from String

$new_string = $my_string.Substring(4)

This particular example will remove the first four characters from the string variable named $my_string and assign the result to a new variable named $new_string.

Method 2: Remove First N Characters from Each Line in File

(gc original_file.txt) | % {$_.Substring(4)} | sc new_file.txt

This particular example removes the first four characters from each line in the file named original_file.txt and stores the results in a new file named new_file.txt.

Note the following:

  • gc is an alias for Get-Content
  • % is an alias for ForEach-Object
  • sc is an alias for Set-Content

Note that in both of these examples we use the Substring() method to return an entire string starting from a specific index.

By using Substring(4) we are able to return an entire string starting from index position 4, which effectively trims the first 4 characters from a string.

The following examples show how to use each of these methods in practice.

Example 1: Remove First N Characters from String in PowerShell

Suppose that we have a variable named $my_string in PowerShell that contains the following string

  • A005Zach

We can use the following syntax to create a new variable named $new_string that removes the first four characters from this original string:

$my_string = "A005Zach"
$new_string = $my_string.Substring(4)

The following screenshot shows how to use this syntax in practice:

PowerShell remove first n characters from string

Notice that this returns the string from the variable named $my_string with the first four characters removed.

Example 2: Remove First N Characters from Each Line in File in PowerShell

Suppose that we have a file named employees.txt that contains the employee ID’s of various employees at some company.

We can use the Get-Content cmdlet to view each of the lines in this file:

We can use the following syntax to remove the first four characters from each line in the file and save the results to a new file named employees_new.txt:

(gc employees.txt)| % {$_.Substring(4)} | sc employees_new.txt

The following screenshot shows how to use this syntax in practice:

PowerShell remove first N characters from string

When we use the Get-Content cmdlet to view the contents of the new employees_new.txt file we can see that the first four characters have been removed from each line in the original file.

Note: Feel free to replace the 4 in the Substring method with whatever number you would like depending on the number of characters you’d like to remove from the beginning of the string.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Trim Leading Spaces from String
PowerShell: How to Extract Text Between Two Strings
PowerShell: How to Extract Only Numbers from String

Leave a Reply