Saksham
Saksham

Reputation: 9380

Powershell replace double quotes with single in a string

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?

Upvotes: 5

Views: 23419

Answers (3)

Daw Pod
Daw Pod

Reputation: 1

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

Upvotes: 0

Saksham
Saksham

Reputation: 9380

It should be

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

Upvotes: 13

user2630614
user2630614

Reputation:

this because the escaping character is `

in the following a working example

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

Upvotes: 6

Related Questions