5

I just can't get it right

The below statement throws an exception and I cannot get the correct format

$appendedQry = $appendedQry -replace "\"","'" 

What would be the correct syntax?

3 Answers 3

13

It should be

$appendedQry = $appendedQry -replace '"','''' 
Sign up to request clarification or add additional context in comments.

1 Comment

Personally, I like -replace '"',"'". ;)
6

this because the escaping character is `

in the following a working example

$appendedQry = "`"asd" echo $appendedQry $appendedQry = $appendedQry -replace "`"", "'" echo $appendedQry 

Comments

0

I was replacing all occurances of the string in a file, using command

powershell -Command "(gc c:\input.txt) -replace 'aaa', 'bbb' | Out-File c:\output.txt" 

To replace double quotes I need to do some trick - use the variable:

$ToReplace = "\" + """" $command = "(gc c:\input.txt) -replace '" + $ToReplace + "', 'bbb' | Out-File c:\output.txt" powershell -Command $command 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.