This question was asked many times on SO and yet...
All I've seen were solutions where the input string has to be modified. Either by replacing all double quotes with single quotes or by using backticks.
But I have no control over the input string since I have no access to the source. I cannot change Hello "W"orld to Hello 'W'orld or Hello """W"""orld
What I can do is to wrap the whole string with any escaping characters. For example with single quotes around 'Hello "W"orld'. But none of thoses escaping mechanisms I tried worked. And I can change my PowerShell script
Q: How can I pass a string with double quotes to PowerShell as argument and retain the quotes?
How to reproduce
Save this
cls write-host $args[0]as PowerShell script
echoArgs1.ps1on your desktop.Open a CMD window, navigate to your desktop folder and enter
powershell -file echoArgs1.ps1 "Hello "W"orld"


powershell -file echoArgs1.ps1 "Hello """W"""orld"should work (note tripled inner"s).C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exeand under Arguments I put-file "D:\echoArgs.ps1" "$(CurText)": Then i select any text in Visual Studio and execute that command. The crucial part is probably"$(CurText)"