Recently, one of my team members was required to trim a string to a specific length while working with strings in PowerShell. I suggested various methods to do it. In this tutorial, I will explain how to trim a variable to a specific length in PowerShell with examples.
To trim a variable to a specific length in PowerShell, use the Substring() method. For instance, to trim a string to 10 characters, you can use:
$string = "California"
$trimmedString = $string.Substring(0, [math]::Min(10, $string.Length))
Write-Output $trimmedString # Output: "California"This code ensures that the string is trimmed to a maximum of 10 characters, or the full string length if it’s shorter than 10 characters.
Trim a Variable to a Specific Length in PowerShell
To trim a string to a specific length in PowerShell, you can use the Substring() method or the -replace operator. These methods allow you to specify the desired length and extract the part of the string you need.
Method 1: Using Substring()
The Substring() method in PowerShell extracts a portion of a string based on the specified starting position and length.
Let me show you an example.
Example:
Let’s say we have a string, and we want to trim it to a length of 10 characters. Then you can write the PowerShell script below.
$string = "California"
$trimmedString = $string.Substring(0, [math]::Min(10, $string.Length))
Write-Output $trimmedString # Output: "California"In this example, Substring(0, 10) extracts the first 10 characters of the string. The [math]::Min(10, $string.Length) ensures that if the string is shorter than 10 characters, it will return the entire string without error.
Here is the exact output you can see in the screenshot below:

Check Convert a String to Title Case in PowerShell
Method 2: Using the -replace Operator
The -replace operator in PowerShell can be used with regular expressions to truncate a string to a specific length.
Let me show you an example.
Example:
Suppose we want to trim a string to 5 characters; then you can write the script below.
$string = "New York"
$trimmedString = $string -replace "^(.{5}).*", '$1'
Write-Output $trimmedString # Output: "New Y"In this example, -replace "^(.{5}).*", '$1' uses a regular expression to match the first 5 characters of the string and replace the rest with an empty string.
Here is the exact output you can see in the screenshot below:

Read PowerShell String Replace
Method 3: Using the .NET String Method
You can also use the .NET String class methods directly in PowerShell to achieve the same result.
Example:
Let’s trim a string to 8 characters.
$string = "Washington"
$trimmedString = [string]::Concat($string.ToCharArray()[0..([math]::Min(7, $string.Length - 1))])
Write-Output $trimmedString # Output: "Washing"In this example, ToCharArray() converts the string to an array of characters, and [0..([math]::Min(7, $string.Length - 1))] selects the first 8 characters. String::Concat then joins these characters back into a string.
Read Convert String to Camel Case in PowerShell
PowerShell Trim Variable Length Examples
Now, let me show a few real examples of how to trim a variable with a specific length in PowerShell.
Example-1: Truncate File Names
If you have a file name and want to ensure it does not exceed a certain length, then you can write a script like below.
$fileName = "AnnualReport2024.pdf"
$maxLength = 16
$trimmedFileName = $fileName.Substring(0, [math]::Min($maxLength, $fileName.Length))
Write-Output $trimmedFileName # Output: "AnnualReport2024"Here, Substring(0, $maxLength) ensures the file name does not exceed 15 characters.
Here is the output in the screenshot below:
![$fileName = "AnnualReport2024.pdf" $maxLength = 15 $trimmedFileName = $fileName.Substring(0, [math]::Min($maxLength, $fileName.Length)) Write-Output $trimmedFileName # Output: "AnnualReport2024"](https://powershellfaqs.com/wp-content/uploads/2024/09/Trim-a-Variable-to-a-Specific-Length-in-PowerShell-1.jpg)
Example-2: Format User Input
For a scenario where user input needs to be truncated to fit a specific field length:
$userInput = "Elizabeth Johnson"
$maxLength = 10
$formattedInput = $userInput.Substring(0, [math]::Min($maxLength, $userInput.Length))
Write-Output $formattedInput # Output: "Elizabeth"Conclusion
In this tutorial, I explained how to trim a variable to a specific length in PowerShell using different methods such as the Substring() method, the -replace operator, or the .NET String class methods, etc. I hope the examples also help you.
You may also like:
- PowerShell New-Variable Cmdlet
- Trim Variable Before Character in PowerShell
- Trim Variable After Character in PowerShell
- Trim the First 4 Characters from a Variable in 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.