In PowerShell, you might encounter situations where you need to trim the first few characters from a string. One of my team members has exactly similar requirements. In this tutorial, I’ll explain how to trim the first 4 characters from a variable in PowerShell using different methods and practical examples.
To trim the first 4 characters from a variable in PowerShell, use the Substring() method. For example, to remove the first 4 characters from a string, you can use:
$string = "Washington"
$trimmedString = $string.Substring(4)
Write-Output $trimmedString # Output: "ington"This code removes the first 4 characters by starting the substring at index 4, returning the remainder of the string.
Trim the First 4 Characters from a Variable in PowerShell
To trim the first 4 characters from a string, you can use methods such as Substring(), the -replace operator, or the TrimStart() method in combination with other string functions. These methods allow you to easily remove the specified number of characters from the beginning of the string.
Let me show you each method with examples.
Method 1: Using Substring()
The Substring() method in PowerShell is the best way to trim characters from the start of a string by specifying the starting index.
Let me show you an example.
Example:
Let’s say we have a string, and we want to trim the first 4 characters using PowerShell.
$string = "Washington"
$trimmedString = $string.Substring(4)
Write-Output $trimmedString # Output: "ington"In this example, Substring(4) starts the substring at the 5th character (index 4), effectively removing the first 4 characters.
I executed the above PowerShell script, and you can see the exact output in the screenshot below:

Check out PowerShell If Variable Equals
Method 2: Using the -replace Operator
The -replace operator can be used with regular expressions to remove a specific number of characters from the beginning of a PowerShell string.
Here is an example.
Example:
Suppose we want to trim the first 4 characters from a string, then you can write the below script.
$string = "California"
$trimmedString = $string -replace "^.{4}", ""
Write-Output $trimmedString # Output: "fornia"In this example, -replace "^.{4}", "" uses a regular expression to match the first 4 characters (^.{4}) and replaces them with an empty string.
You can see the exact output in the screenshot below:

Check out Remove Whitespace from Strings in PowerShell
Method 3: Using TrimStart() with Substring()
While TrimStart() is typically used for removing specific characters, you can combine it with Substring() to remove a fixed number of characters.
Here is an example.
Example:
Let’s trim the first 4 characters from a string using a combination of methods.
$string = "Texas"
$trimmedString = $string.TrimStart($string.Substring(0, 4).ToCharArray())
Write-Output $trimmedString # Output: "s"In this example, Substring(0, 4) extracts the first 4 characters, and TrimStart() removes those characters from the beginning of the string.
Read Check if a String Contains a Space in PowerShell?
PowerShell: Trim the First 4 Charcters from a Variable Examples
Now, let me show you two real examples of trimming the first four characters from a variable. You will get the requirements while working in PowerShell scripts.
Example-1: Clean User Input
When dealing with user input that includes a prefix, you might want to remove the prefix to get the actual data.
$userInput = "ID1234JohnDoe"
$trimmedInput = $userInput.Substring(4)
Write-Output $trimmedInput # Output: "34JohnDoe"Here, Substring(4) removes the first 4 characters, which might be a common prefix like “ID12”.
Here is the output you can see in the screenshot below:

Example-2: Process File Names
If you have a file name with a specific prefix that needs to be removed:
$fileName = "2024_Report.pdf"
$trimmedFileName = $fileName.Substring(4)
Write-Output $trimmedFileName # Output: "Report.pdf"In this example, Substring(4) removes the first 4 characters, which could be a year or other metadata.
Conclusion
In this tutorial, I explained how to trim the first 4 characters from a variable in PowerShell using various methods, such as the Substring() method, the -replace operator, or a combination of TrimStart() and Substring(), etc. For each method, I have also shown a few real examples.
You may also like the following tutorials:
- Increment a String Variable by 1 in PowerShell
- Find a String in a File and Return the Line Using PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.