Thursday, November 21, 2013

Escaping double quotes in PowerShell strings

If you have to escape double quotes in PowerShell strings, the easiest way to do so is to escape them using the "`" (backtick character) in all your strings.

Therefore, if you have a very long string that requires inclusion of double quotes, you will have to include a ` character before the beginning of the string (the 1st double quote) and a final ` character before the final double quote.

Here is an example:
$IISCmd = "Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter `"system.applicationHost/applicationPools`" -name `".`" -value @{name=`"$AppPoolName`";managedRuntimeVersion=`"$AppPoolVersion`"}"

You can also use an IDE such as PowerShell ISE or PowerGui to make sure that the formatting of your final PowerShell string is correctly escaped, but at least this example should hopefully help you deal with these normally problematic strings!

No comments:

Post a Comment