Abraham Lincoln made the famous quote that it is impossible to verify the accuracy of information on the Internet. Well, OK he didn't but it was a semi-funny intro on the subject of quotes…..
There are two different types quotes that we can use in PowerShell.:
Single quotes --> ‘
Double quotes –> “
This is a thankful change for those administrators who used to write VBScript.
Legacy VBScript Escape
In VBScript when we were trying to escape a quote, we had to do it with another quote. This lead to very complicated and confusing strings that were fun to manipulate. Well it seems fun now, at the time “not so much”. For example if we want to echo This is the “phrase” then that requires some manipulation. As you can see below, simply adding quotes around the phrase does not work…
StrPhrase = "This is the "phrase” “
When executed, it throws: Error 800A0401 Microsoft VBScript compilation error: Expected end of statement Microsoft VBScript compilation error
To escape the “ we have to prefix it with another “. This is an updated version, where the quotes are correctly escaped:
StrPhrase = "This is the ""phrase"" "
To make it more interesting, let’s add some extra text, another set of quoted text and quote at the end.
strPhrase = "This is the ""phrase"" " + " And we have ""more"" " + "Text To Add..." + "And single quote ' plus a double quote "" "
VBScript allowed two delimiters: the single quotation mark (') and the REM statement. Both examples are highlighted below.
WMI had the nous to change this up, and use different types of quotes for different purposes.
WMI Query Language
When making a WMI Query language (WQL) call in VBScript we would then use the single quotes. As an example:
Set objEvents = objWMIService.ExecNotificationQuery _ ("SELECT * FROM __InstanceModificationEvent WITHIN 10 WHERE " & _ "TargetInstance ISA 'Win32_Service'" & _ " AND TargetInstance._Class = 'win32_TerminalService'")
Note the single quotes in the last portion: Class = 'win32_TerminalService'
PowerShell also learned from the VBScript quoting fun!
PowerShell Quotes In Action
Lets look at what the different quotes in PowerShell. The best way to showcase this is with an example. We shall save a text phrase into the variable $Phrase. It will contain the word zorg.
$Phrase = "zorg"
Watch what happens in the below examples when we try and expand the variable using Write-Host. The first example uses double quotes, with the second using single.
Write-Host "This is the contents of the variable: $Phrase"
Write-Host 'This is the contents of the variable: $Phrase'
Notice the difference there? Look at the underlined items in the below, and compare:
Look at what was returned in the second response. We did not perform variable expansion and $Phrase was treated as a piece of text.
This can be used to control what is displayed when you may not want a variable to be expanded for a particular reason.
Do leave a comment for the other reasons that you are using each of them!
Bonus Legacy CMD Escape Antics
Since this is relevant to the crusty older blog reader, the cmd example is added at the end.
Escaping was something we were doing way back then. If you consider the example command below that echos the name of the file:
FOR %f IN (C:Scriptsoldskool.vbs) DO @ ECHO %f
We can run that command successfully. But if we save it to a .bat file, then it does not work. The underlined result shows the command working. Then we display the contents of the .bat file, which is again the same command. Finally the command is executed, and we get an error which is indicated by the big red arrow below:
We need to escape the % symbol. The command when placed into a batch file must look like this:
FOR %%f IN (C:Scriptsoldskool.vbs) DO @ ECHO %%f
Note that the percent symbols are doubled up - %%.
And then it runs as expected.