Home » PowerShell » Replace Single Quote in PowerShell

Replace Single Quote in PowerShell

Use the PowerShell String replace() method or replace operator to replace the single quote in PowerShell with empty space or double quotes. e.g. $str.replace("'",'') replace the single quotes with an empty string.

The replace() method returns a string where all single quote is replaced.

In this article, we will discuss how to use PowerShell replace() method or replace operator to replace single quotes in a string.

Use replace() to Replace Single Quote 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 = "'C#','PowerShell','NodeJs'"

# Replace all single quotes with blank 
$str.Replace("'",'')                    

The above PowerShell script uses the replace() method to replace all single quotes in a string with blank or empty spaces.

PowerShell replace() takes 2 arguments:

  1. the substring to search for in the given string
  2. the replacement string for the found text

The PowerShell replace() method returns a new string where all the single quotes in a string are replaced by the blank.

The output of the above script to replace all single quotes with blank is:

PowerShell Replace Single Quote with Blank
PowerShell Replace Single Quote with Blank

Replace Single Quote with Double Quotes using PowerShell

Using the PowerShell replace() method, it can replace all single quotes in a string with double quotes.

$str = "'C#','PowerShell','NodeJs'"

# Replace all single quotes with double quotes
$str.Replace("'",'"')

In the above PowerShell script, replace() method replaces all single quotes with double quotes. It takes a single quote as the first argument to find in a string and a second argument as a double quote to replace with found text.

The output of the above PowerShell script to replace all single quotes in a string with double quotes is:

PS C:\> $str = "'C#','PowerShell','NodeJs'"    

PS C:\> $str.Replace("'",'"') 
"C#","PowerShell","NodeJs"

Cool Tip: How to replace all space in a string with newline using PowerShell!

Use replace Operator to Replace Single Quote in PowerShell

Using the PowerShell replace operator, you can replace all single quotes in a string with blank or double quotes.

The replace operator in PowerShell takes two arguments; string to find for in a string and replacement string for the found text.

$str = "'C#','PowerShell','NodeJs'"
      
# Replace all single quotes with empty
$str -replace "'", ''     

# Replace all single quotes with double quotes
$str -replace "'", '"'

The above PowerShell script uses the replace operator to replace all single quotes in a string with empty space or blank.

replace operator takes 2 arguments:

  1. the substring to search for in a string
  2. the replacement string

In the first example, replace operator replaces all single quotes with blank, and in the second example, it replaces all single quotes with double quotes.

The output of the above Powershell script to replace a single quote in a string is:

PS C:\> $str = "'C#','PowerShell','NodeJs'"                                                                             

PS C:\> $str -replace "'", ''                                                                                           
C#,PowerShell,NodeJs

 PS C:\>$str -replace "'", '"'
"C#","PowerShell","NodeJs"

Cool Tip: How to replace double quotes in a string using PowerShell!

Conclusion

I hope the above article on how to replace all single quotes with blank or double 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.

Leave a Comment