Use the PowerShell String replace()
method or replace
operator to replace the double quotes in PowerShell with empty spaces or single quotes. e.g. $str.replace('"',"'")
replace the double quotes with a single quote.
The replace() method returns a string where all double quotes are replaced.
In this article, we will discuss how to use PowerShell replace() method or replace operator to replace double quotes in a string.
Use replace() to Replace Double Quotes with blank in PowerShell
PowerShell String built-in replace() method takes two arguments; a string to search and a string to replace with the found text.
$str = $str = 'This is an "embedded quote" in a string.' # Replace all double quotes with blank $str.Replace('"','')
The above PowerShell script uses the replace()
method to replace all double quotes in a string with blank or empty spaces.
The PowerShell replace()
takes 2 arguments:
- the substring to search for in the string. In the above example, it is a double quote (“)
- the replacement string for the found text. In the above example, it is empty space.
The PowerShell replace() method returns a new string where all the double quotes in a string are replaced by the blank.
The output of the above script to replace all double quotes with blank is:
Replace Double Quotes with Single Quotes using PowerShell
Using the PowerShell replace()
method, it can replace all double quotes in a string with single quotes.
$str = 'This is an "embedded quote" in a string.' # Replace all double quotes with single quote $str.Replace('"',"'")
In the above PowerShell script, replace() method replaces all double quotes with single quotes. It takes double quotes as the first argument to find in a string and a second argument as a single quote to replace with found text.
The output of the above PowerShell script to replace all double quotes in a string with single quotes is:
PS C:\> $str = 'This is an "embedded quote" in a string.'
PS C:\> $str.Replace('"',"'")
This is an 'embedded quote' in a string.
Cool Tip: How to replace all space in a string with newline using PowerShell!
Use replace Operator to Replace Double Quotes in PowerShell
Using the PowerShell replace operator, you can replace all double quotes in a string with blank or single quotes.
The replace operator in PowerShell takes two arguments; string to find for in a string and replacement string for the found text.
$str = 'This is an "embedded quote" in a string.' # Replace all double quotes with empty $str -replace '"','''' # Replace all double quotes with single quotes $str -replace '"',"'"
The above PowerShell script uses the replace operator to replace all double quotes in a string with empty space or blank.
replace operator takes 2 arguments:
- the substring to search for in a string
- the replacement string
In the first example, replace
operator replaces all double quotes with blank, and in the second example, it replaces all double quotes with single quotes.
The output of the above Powershell script to replace a double quote in a string is:
PS C:\> $str = 'This is an "embedded quote" in a string.'
PS C:\> $str -replace '"',''
This is an embedded quote in a string.
PS C:\> $str -replace '"',"'"
This is an 'embedded quote' in a string.
Cool Tip: How to replace single quotes in a string using PowerShell!
Conclusion
I hope the above article on how to replace all double quotes with blank or single quotes in a string using PowerShell replace() method is helpful to you.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.