When working with strings in PowerShell, you might need to trim or extract parts of a string before a specific character. One of my team members received this exact requirement. In this tutorial, I’ll explain how to trim a variable before a specific character in PowerShell using various methods and practical examples.
To trim a variable before a specific character in PowerShell, use the Substring() method combined with IndexOf(). For example, to trim everything before the “@” character in an email address, use:
$email = "alice.johnson@example.com"
$position = $email.IndexOf("@")
$trimmedString = $email.Substring($position + 1)
Write-Output $trimmedString # Output: "example.com"This code finds the position of the “@” character and extracts the substring after it, effectively trimming the variable before the specified character.
Trimming Before a Specific Character in PowerShell
To trim a string before a specific character, you can use methods such as 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 extracts a portion of a string, and IndexOf() helps find the position of a specific character.
Example:
Let’s say we have a string containing a full name, and we want to trim everything before the first space.
$string = "John Doe"
$position = $string.IndexOf(" ")
$trimmedString = $string.Substring($position + 1)
Write-Output $trimmedString # Output: "Doe"In this example, IndexOf(" ") finds the position of the first space, and Substring($position + 1) extracts the part of the string after the space.
You can see the output in the screenshot below:

Check out Remove Whitespace from Strings in PowerShell
Method 2: Using the -split Operator
The -split operator splits a string into an array based on a delimiter in PowerShell. You can then select the part of the array you need.
Example:
Suppose we have an email address and want to trim everything before the “@” symbol.
$email = "alice.johnson@example.com"
$trimmedString = ($email -split "@")[1]
Write-Output $trimmedString # Output: "example.com"Here, -split "@" splits the email address into an array with two elements: alice.johnson and example.com. Selecting the second element ([1]) gives us the part after the “@”.
I executed the above PowerShell script using VS code, and you can see the output in the screenshot below:

Read Check if a String Contains a Space in PowerShell
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 before a specific character.
Example:
Let’s trim everything before the first comma in a string using PowerShell regular expressions.
$string = "New York, NY, USA"
$trimmedString = $string -replace ".*?,", ""
Write-Output $trimmedString # Output: " NY, USA"In this example, -replace ".*?,", "" uses a regular expression to find any characters followed by a comma (.*?,) and replaces it with an empty string.
You can see the output in the screenshot below:

Check out Remove Newline from String in PowerShell
PowerShell Trim Variable Before Character Examples
Now, let me show you some practical examples of trimming variables before the characters in PowerShell.
Example 1: Extract Domain Names
Use the PowerShell script if you have a URL and want to extract the domain name.
$url = "https://www.example.com/page"
$trimmedString = $url.Substring($url.IndexOf("://") + 3)
$domain = $trimmedString.Split("/")[0]
Write-Output $domain # Output: "www.example.com"Here, IndexOf("://") finds the position of ://, and Substring($position + 3) extracts everything after it. The Split("/") method then gets the domain name.
Here is the output in the screenshot below:

Example 2: Clean User Input
For a scenario where user input includes a delimiter, like a colon, and you need the part after it. Here is the complete script.
$userInput = "username:password"
$trimmedInput = $userInput.Split(":")[1]
Write-Output $trimmedInput # Output: "password"Conclusion
In this tutorial, I explained how to trim a variable before a specific character in PowerShell using various methods. You can use methods like Substring() with IndexOf(), the -split operator, or regular expressions, etc. Also, I provided two real-time examples related to the PowerShell trim variable before a character.
You may also like:
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.