Recently, one of the team members was required to remove characters from a string. For this I suggested different methods to do so using PowerShell. In this tutorial, I will show you how to remove characters from strings in PowerShell using various methods with examples.
To remove characters from a string in PowerShell, you can use the -replace operator, which utilizes regular expressions for pattern matching. For example, to remove spaces and hyphens from city names, you can use the following code: $cities = $cities -replace “[- ]”, “”. This command searches for spaces and hyphens in each string and replaces them with an empty string, effectively removing them.
Method 1: Using the -replace Operator
The -replace operator is one of the best ways to remove characters from a string in PowerShell. It uses regular expressions to find and replace patterns in strings.
Example: Removing Spaces and Hyphens
Let’s say we have a list of city names with spaces and hyphens that we want to remove:
$cities = @("New-York", "Los Angeles", "San Francisco", "Las-Vegas")
$cities = $cities -replace "[- ]", ""
$citiesOutput:
NewYork
LosAngeles
SanFrancisco
LasVegasIn this example, the -replace operator searches for spaces (" ") and hyphens ("-") and replaces them with an empty string ("").
You can see the exact output in the screenshot below after I executed the above PowerShell script using VS code.

Check out Find Lines Starting with a Specific String in PowerShell
Method 2: Using the Substring() Method
The Substring() method is useful when you know the exact positions of the characters you want to remove. Here is an example of how to use the substring() method to remove characters from a string in PowerShell.
Example: Removing the First Three Characters
Suppose we want to remove the first three characters from each city name:
$cities = @("NewYork", "LosAngeles", "SanFrancisco", "LasVegas")
$cities = $cities | ForEach-Object { $_.Substring(3) }
$citiesOutput:
York
Angeles
Francisco
VegasHere, Substring(3) removes the first three characters from each string.
I executed the above PowerShell script using VS code, and you can see the output in the screenshot below:

Method 3: Using the Trim() Methods
PowerShell provides several Trim() methods to remove characters from the beginning and end of strings. Here is an example.
Example: Trimming Leading and Trailing Spaces
Let’s consider city names with leading and trailing spaces:
$cities = @(" New York ", " Los Angeles ", " San Francisco ", " Las Vegas ")
$cities = $cities | ForEach-Object { $_.Trim() }
$citiesOutput:
New York
Los Angeles
San Francisco
Las VegasThe Trim() method removes all leading and trailing spaces from each string.
Check out Check if a String Starts with a Number in PowerShell
Method 4: Using Split() and Join()
The Split() and Join() methods can be combined to remove specific characters by splitting the string into an array and then rejoining it in PowerShell.
Here is an example to understand it better.
Example: Removing Commas
Suppose we have city names with commas that we want to remove:
$cities = @("New,York", "Los,Angeles", "San,Francisco", "Las,Vegas")
$cities = $cities | ForEach-Object { ($_.Split(",") -join "") }
$citiesOutput:
NewYork
LosAngeles
SanFrancisco
LasVegasIn this example, Split(",") splits each string into an array of substrings separated by commas, and -join "" rejoins them without any separator.
You can see the screenshot below for the exact output:

Read Check if a String Contains Only Numbers in PowerShell
Method 5: Using Regular Expressions with -match
Regular expressions can also be used with the -match operator to remove characters from a string based on patterns in PowerShell.
Let me show you an example.
Example: Removing All Non-Alphanumeric Characters
Suppose we want to remove all non-alphanumeric characters from city names:
$cities = @("New York!", "Los Angeles@", "San Francisco#", "Las Vegas$")
$cities = $cities | ForEach-Object { $_ -replace "[^a-zA-Z0-9]", "" }
$citiesOutput:
NewYork
LosAngeles
SanFrancisco
LasVegasThe -replace "[^a-zA-Z0-9]", "" pattern removes all characters that are not letters or digits.
Conclusion
In this tutorial, I have explained different methods to remove characters from strings in PowerShell, like using the -replace operator, Substring(), Trim(), Split() and Join(), or regular expressions, etc. I hope all these real examples will help you with this.
You may also like the following tutorials:
- Convert String to JSON in PowerShell
- PowerShell: IsNullOrEmpty vs IsNullOrWhiteSpace
- Replace Multiple Characters in a String in PowerShell
- Replace Placeholders in Strings 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.