PowerShell: How to Replace Double Quotes in String


Often you may want to replace double quotes in a string in PowerShell.

Here are two common ways to do so:

Method 1: Replace double quotes with single quotes

 $original_string -replace '"', ''''

Method 2: Replace double quotes with blank

 $original_string -replace '"', ''

The following examples show how to use each method in practice.

Example 1: Replace Double Quotes with Single Quotes in PowerShell

The following example shows how to replace double quotes with single quotes in a string in PowerShell:

$original_string = 'My best advice: "Live in the moment."'
$original_string -replace '"', '''' 

The following screenshot shows how to use this syntax in practice:

PowerShell replace double quotes with single quotes

Notice that the double quotes in the string have been replaced with single quotes.

The original string was: My best advice: “Live in the moment.”

The new string is: My best advice: ‘Live in the moment.’

Example 2: Replace Double Quotes with Blank in PowerShell

The following example shows how to replace double quotes with blanks in a string in PowerShell:

$original_string = 'My best advice: "Live in the moment."'
$original_string -replace '"', '' 

The following screenshot shows how to use this syntax in practice:

Notice that the double quotes in the string have been replaced with blanks.

This has the same effect as simply removing the double quotes from the string entirely.

The original string was: My best advice: “Live in the moment.”

The new string is: My best advice: Live in the moment.

Note: In these examples we replaced the double quotes with either single quotes or blanks, but you can use similar syntax to replace the double quotes with any characters that you’d like using the -replace operator.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Replace Every Occurrence of String in File
PowerShell: How to Replace Special Characters in String
PowerShell: How to Concatenate Strings

Leave a Reply