How to Trim the Last 4 Characters from a Variable in PowerShell?

In PowerShell, you might need to trim the last few characters from a string for various reasons, such as processing file names, user input, or any data where the trailing characters are not needed. This is a very common requirement among PowerShell developers. In this tutorial, I’ll explain how to trim the last 4 characters from a variable in PowerShell using different methods and examples.

To trim the last 4 characters from a variable in PowerShell, use the Substring() method. For example, to remove the last 4 characters from a string, you can use:

$string = "California"
$trimmedString = $string.Substring(0, $string.Length - 4)
Write-Output $trimmedString  # Output: "Califor"

This code extracts the substring from the beginning up to the length minus 4 characters, effectively removing the last 4 characters.

Trim the Last 4 Characters from a PowerShell Variable

To trim the last 4 characters from a string in PowerShell, you can use methods such as Substring(), the -replace operator, or the TrimEnd() method in combination with other string functions. These methods allow you to easily remove the specified number of characters from the end of the string.

Now, let me show you each method with examples.

Method 1: Using Substring()

The Substring() method is the best and recommended way to trim characters from the end of a string by specifying the starting index and length. Let me show you an example to help you understand better.

Example:

Let’s say we have a string, and we want to trim the last 4 characters.

$string = "Washington"
$trimmedString = $string.Substring(0, $string.Length - 4)
Write-Output $trimmedString  # Output: "Washin"

In this example, Substring(0, $string.Length - 4) extracts the string from the beginning up to the length minus 4 characters, effectively removing the last 4 characters.

You can see the exact output in the screenshot below:

powershell trim last 4 characters from variable

Check out Trim Variable Length in PowerShell

Method 2: Using the -replace Operator

The -replace operator can be used with regular expressions to remove a specific number of characters from the end of a PowerShell string.

Example:

Let me show you an example.

Suppose we want to trim the last 4 characters from a string.

$string = "California"
$trimmedString = $string -replace ".{4}$", ""
Write-Output $trimmedString  # Output: "Califo"

In this example, -replace ".{4}$", "" uses a regular expression to match the last 4 characters (.{4}$) and replaces them with an empty string.

You can see the output in the screenshot below; I executed the above script using VS code.

powershell trim last 4 characters from a variable

Method 3: Using TrimEnd() with Substring()

While TrimEnd() is typically used for removing specific characters, you can combine it with Substring() to remove a fixed number of characters from the end.

These methods are tricky but useful. Here is an example.

Example:

Let’s trim the last 4 characters from a string using a combination of methods.

$string = "Texas"
$trimmedString = $string.TrimEnd($string.Substring($string.Length - 4).ToCharArray())
Write-Output $trimmedString  # Output: "T"

In this example, Substring($string.Length - 4) extracts the last 4 characters, and TrimEnd() removes those characters from the end of the string.

Look at the screenshot below:

Trim the Last 4 Characters from a PowerShell Variable

Read Check if a Variable is Empty in PowerShell

PowerShell: Trim the Last 4 Characters from a Variable Examples

Now, let me show you two real-time examples of trimming the last four characters from a variable in PowerShell.

Example 1: Clean User Input

You might want to remove the suffix to get the actual data when dealing with user input that includes a suffix. You can use the script below:

$userInput = "JohnDoe1234"
$trimmedInput = $userInput.Substring(0, $userInput.Length - 4)
Write-Output $trimmedInput  # Output: "JohnDoe"

Here, Substring(0, $userInput.Length - 4) removes the last 4 characters, which might be a common suffix like “1234”.

Example 2: Process File Names

If you have a file name with a specific suffix that needs to be removed, then you can write the below PowerShell script.

$fileName = "Report2024.pdf"
$trimmedFileName = $fileName.Substring(0, $fileName.Length - 4)
Write-Output $trimmedFileName  # Output: "Report2024"

In this example, Substring(0, $fileName.Length - 4) removes the last 4 characters, which could be a file extension or other metadata.

Conclusion

In this tutorial, I have explained how to trim the last 4 characters from a variable in PowerShell using various methods, such as the Substring() method, the -replace operator, or a combination of TrimEnd() and Substring(), etc. I have also shown you two real examples of this. Do you still have any questions? feel free to 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.