How to Remove Characters from Strings in PowerShell?

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 "[- ]", ""
$cities

Output:

NewYork
LosAngeles
SanFrancisco
LasVegas

In 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.

Remove Characters from Strings in PowerShell

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) }
$cities

Output:

York
Angeles
Francisco
Vegas

Here, 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:

PowerShell Remove Characters from Strings

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() }
$cities

Output:

New York
Los Angeles
San Francisco
Las Vegas

The 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 "") }
$cities

Output:

NewYork
LosAngeles
SanFrancisco
LasVegas

In 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:

Remove Characters from a String in PowerShell

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]", "" }
$cities

Output:

NewYork
LosAngeles
SanFrancisco
LasVegas

The -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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.