PowerShell: remove or replace quotation marks from a variable

I use Get-EventLog to set the variable, and then set another variable with the description of the event ID. Then I use blat.exe to send this information to the group.

Description contains quotation marks. The quotes make blat exit with an error.

Is there a way to remove quotes from an event. Post and replace them with a space or something?

+7
source share
6 answers

I just got it. The number of quotes and double quotes baffled me, but it worked, and blat was not mistaken.

$var -replace '"', "" 

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

+7
source

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

 $Variable.Replace("`"","") 
+13
source

If you use the built-in send-mailmessage (2.0) PowerShell, you can eliminate your dependency on blat.exe and properly deal with this problem without editing the description from the event log.

+3
source

The problem is that a simple replacement clears every character , even if it is spared (doubled). Here are the functions that I created for my use:

  • which removes only orphan quotes.
  • which eludes them

I also made them generalized to control other characters, with optionnal $ charToReplace

 #Replaces single occurences of characters in a string. #Default is to replace single quotes Function RemoveNonEscapedChar { param( [Parameter(Mandatory = $true)][String] $param, [Parameter(Mandatory = $false)][String] $charToReplace ) if ($charToReplace -eq '') { $charToReplace = "'" } $cleanedString = "" $index = 0 $length = $param.length for ($index = 0; $index -lt $length; $index++) { $char = $param[$index] if ($char -eq $charToReplace) { if ($index +1 -lt $length -and $param[$index + 1] -eq $charToReplace) { $cleanedString += "$charToReplace$charToReplace" ++$index ## /!\ Manual increment of our loop counter to skip next char /!\ } continue } $cleanedString += $char } return $cleanedString } #A few test cases : RemoveNonEscapedChar("'st''r'''i''ng'") #Echoes st''r''i''ng RemoveNonEscapedChar("""st""""r""""""i""""ng""") -charToReplace '"' #Echoes st""r""i""ng RemoveNonEscapedChar("'st''r'''i''ng'") -charToReplace 'r' #Echoes 'st'''''i''ng' 


 #Escapes single occurences of characters in a string. Double occurences are not escaped. eg ''' will become '''', NOT ''''''. #Default is to replace single quotes Function EscapeChar { param( [Parameter(Mandatory = $true)][String] $param, [Parameter(Mandatory = $false)][String] $charToEscape ) if ($charToEscape -eq '') { $charToEscape = "'" } $cleanedString = "" $index = 0 $length = $param.length for ($index = 0; $index -lt $length; $index++) { $char = $param[$index] if ($char -eq $charToEscape) { if ($index +1 -lt $length -and $param[$index + 1] -eq $charToEscape) { ++$index ## /!\ Manual increment of our loop counter to skip next char /!\ } $cleanedString += "$charToEscape$charToEscape" continue } $cleanedString += $char } return $cleanedString } #A few test cases : EscapeChar("'st''r'''i''ng'") #Echoes ''st''r''''i''ng'' EscapeChar("""st""""r""""""i""""ng""") -charToEscape '"' #Echoes ""st""r""""i""ng"" EscapeChar("'st''r'''i''ng'") -charToEscape 'r' #Echoes 'st''rr'''i''ng' 
+2
source

None of the above answers worked for me. So I created the following solution ...

Search and replace a single character Quote "" "ascii Character (39) with space" "ascii Character (32)

 $strOldText = [char] 39 $strNewText = [char] 32 $Variable. = $Variable..Replace($strOldText, $strNewText).Trim() 
0
source

easier, use the Trim method (Char []) :
... removes all leading and ending events ...

 eg $your_variable.Trim('"') 

it will store any quotes, escaped or not, inside the string in place:

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

to take into account, it will also remove the orphan double quotation mark:

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

All Articles