PowerShell: remove or replace quote marks from variable

If you use Powershell's built-in send-mailmessage (2.0 required), you can eliminate your dependency on blat.exe and properly handle this issue without editing the description from the event log.


I actually just got it. The number of quotes and double quotes was confusing me, but this has worked and blat did not error.

$var -replace '"', ""

Those quotes are: single, double, single, comma, double, double.


If the variable is a String object then you can do the following:

$Variable.Replace("`"","")

Depending on a case, it might be simpler to use Trim(Char[]) method:
...Removes all leading and trailing occurrences...

e.g. $your_variable.Trim('"')  

It will remove quotes only from start and end of $your_variable. It will keep any quotes, escaped or not, which are inside the text of $your_variable as they were:

PS C:\> $v.Trim('"') # where $v is: "hu""hu"hu'hu"
hu""hu"hu'hu

You can use Trim('"'), Trim("'"), but also both: Trim("`"'")

Note that Trim() does not care if a quote is orphaned, meaning that it will remove ending or starting quote regardless of it having or not a paired quote on the other side of the string.

PS C:\Users\Papo> $hu = "A: He asked `"whos this sofa?`" B: She replied: `"Chris'`""
PS C:\Users\Papo> $hu
A: He asked "whos this sofa?" B: She replied: "Chris'"
PS C:\Users\Papo> $hu.trim('"')
A: He asked "whos this sofa?" B: She replied: "Chris'
PS C:\Users\Papo> # and even worse:
PS C:\Users\Papo> $hu.trim("'`"")
A: He asked "whos this sofa?" B: She replied: "Chris