How to Trim Variable After Character in PowerShell?

While working with PowerShell variables, I got a practical requirement to trim a variable after specific characters. I have used different methods to do this. In this tutorial, I will explain how to trim a variable after a specific character in PowerShell with examples.

To trim a variable after a specific character in PowerShell, you can use the Substring() method combined with IndexOf(). For example, to trim everything after the first space in a string, use:

$string = "John Doe"
$position = $string.IndexOf(" ")
$trimmedString = $string.Substring(0, $position)
Write-Output $trimmedString  # Output: "John"

This code finds the position of the space and extracts the substring before it, effectively trimming the variable after the specified character.

PowerShell Trim Variable After Character

To trim a string after a specific character in PowerShell, you’ll typically use methods like Substring() combined with IndexOf(), or the -split operator. These methods allow you to identify the position of the character and then extract the part of the string you need.

Method 1: Using Substring() and IndexOf()

The Substring() method in PowerShell extracts a portion of a string, and IndexOf() helps find the position of a specific character.

Let me show you an example of this.

Example:

Let’s say we have a string containing a full name, and we want to trim everything after the first space.

$string = "John Doe"
$position = $string.IndexOf(" ")
$trimmedString = $string.Substring(0, $position)
Write-Output $trimmedString  # Output: "John"

In this example, IndexOf(" ") finds the position of the first space, and Substring(0, $position) extracts the part of the string before the space.

The exact output is in the screenshot below after I executed the PowerShell script above.

PowerShell Trim Variable After Character

Check PowerShell If Variable Equals

Method 2: Using the -split Operator

The -split operator splits a string into an array based on a delimiter. You can then select the part of the array you need.

Example:

Suppose we have an email address and want to trim everything after the “@” symbol.

$email = "alice.johnson@example.com"
$trimmedString = ($email -split "@")[0]
Write-Output $trimmedString  # Output: "alice.johnson"

Here, -split "@" splits the email address into an array with two elements: alice.johnson and example.com. Selecting the first element ([0]) gives us the part before the “@”.

Method 3: Using Regular Expressions

Regular expressions provide a powerful way to match and manipulate strings. You can use the -replace operator to remove parts of a string after a specific character.

Example:

Let’s trim everything after the first comma in a string.

$string = "New York, NY, USA"
$trimmedString = $string -replace ",.*", ""
Write-Output $trimmedString  # Output: "New York"

In this example, -replace ",.*", "" uses a regular expression to find a comma followed by any characters (.*) and replaces it with an empty string.

You can see the output in the screenshot below:

powershell trim variable after characters

Check out How to Clear Variables in PowerShell?

Trim Variable After Character in PowerShell Examples

Now, let me show you a few real examples where I will show you how to trim variable after character in PowerShell. As a PowerShell developer, I am sure you must have come across these requirements.

Example 1: Extract File Names

If you have a file path and want to extract just the file name, then you can use the below Powershell script.

$filePath = "C:\Users\JohnDoe\Documents\report.pdf"
$fileName = $filePath.Substring($filePath.LastIndexOf("\") + 1)
Write-Output $fileName  # Output: "report.pdf"

Here, LastIndexOf("\") finds the position of the last backslash, and Substring() extracts everything after it.

Example 2: Clean User Input

For a scenario where user input includes a delimiter, like a colon, and you need the part before it, then you can write the script like below:

$userInput = "username:password"
$trimmedInput = $userInput.Split(":")[0]
Write-Output $trimmedInput  # Output: "username"

Here is the output you can see in the screenshot below:

Trim Variable After Character in PowerShell

Conclusion

In this tutorial, I explained how to trim a variable after a specific character in PowerShell using different useful methods like Substring() with IndexOf(), the -split operator, or regular expressions. For each method, I have also provided examples.

If you still have 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.