How to Remove the Last Character from a String in PowerShell?

One of my team members tried removing the last character from a string. I suggested different approaches. In this tutorial, I will explain several methods to remove the last character from a string in PowerShell with examples.

To remove the last character from a string in PowerShell using the Substring method, you can utilize the following approach: $string = $string.Substring(0, $string.Length - 1). This method works by extracting a portion of the string starting from the first character (index 0) and ending just before the last character, effectively trimming off the unwanted last character. For example, if $originalString = "Hello, PowerShell!", then $trimmedString = $originalString.Substring(0, $originalString.Length - 1) will result in Hello, PowerShell.

Remove the Last Character from a String in PowerShell

You might need to trim the last character from a string in many scenarios. For instance, you might be processing file paths, user inputs, or data from a database where an extra character needs to be removed for proper formatting or further processing.

Let me show you different methods to remove the last character from a string using PowerShell.

Method 1: Using the Substring() Method

The Substring method is the best way to remove the last character from a string in PowerShell. This method extracts a portion of the string starting from a specified index and continuing to the end of the string.

Syntax

Here is the syntax:

$string = $string.Substring(0, $string.Length - 1)

The Substring method takes two parameters: the starting index and the length of the substring. By setting the length to string.Length - 1, we effectively exclude the last character.

Example

Here is an example and the complete script.

$originalString = "Hello, PowerShell!"
$trimmedString = $originalString.Substring(0, $originalString.Length - 1)
Write-Output $trimmedString  # Output: Hello, PowerShell

I executed the above PowerShell script using VS code and you can see the output in the screenshot below:

Remove the Last Character from a String in PowerShell

Read Remove Whitespace from Strings in PowerShell

Method 2: Using the -replace Operator

The -replace operator in PowerShell uses regular expressions to find and replace patterns in a string. This method can be used to remove the last character.

Syntax

Here is the syntax:

$string = $string -replace ".$", ""

In the regular expression ".$", the . matches any character, and $ asserts the position at the end of the string. The -replace operator then replaces this match with an empty string, effectively removing the last character.

Example

Here is an example.

$originalString = "Hello, PowerShell!"
$trimmedString = $originalString -replace ".$", ""
Write-Output $trimmedString  # Output: Hello, PowerShell

You can see the output in the screenshot below:

PowerShell Remove the Last Character from a String

Method 3: Using TrimEnd() Method

The TrimEnd() method in PowerShell is another way to remove characters from the end of a string, although it’s more commonly used to remove specific characters like spaces or punctuation.

Syntax

Here is the syntax:

$string = $string.TrimEnd("<character>")

The TrimEnd() method removes all instances of the specified character(s) from the end of the string. To remove just the last character, you need to specify the exact character you want to remove.

Example

Let me show you an example. Here is the complete PowerShell script.

$originalString = "Hello, PowerShell!"
$trimmedString = $originalString.TrimEnd('!')
Write-Output $trimmedString  # Output: Hello, PowerShell

You can see the output in the screenshot below:

PowerShell Remove the Last Character from a String examples

Check out Remove Newline from String in PowerShell

Method 4: Using Array Slicing

PowerShell allows you to treat strings as arrays of characters, which can be sliced to remove the last character.

Syntax

Here is the syntax:

$string = $string[0..($string.Length - 2)] -join ''

This method creates a range from the first character (index 0) to the second-to-last character (string.Length - 2), effectively excluding the last character. The -join '' part concatenates the array back into a string.

Example

Here is an example.

$originalString = "Hello, PowerShell!"
$trimmedString = $originalString[0..($originalString.Length - 2)] -join ''
Write-Output $trimmedString  # Output: Hello, PowerShell

Here is the output you can see the screenshot below:

Remove Last Character from a String in PowerShell

Conclusion

In this tutorial, I explained how to remove the last character from a string in PowerShell in several ways. There are various methods like the Substring, the -replace operator, TrimEnd, or array slicing, etc.

Do you still have some questions? Leave a comment below.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.