Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 34890

PowerShell Quotes – To Expand Or Not Expand, That Is The Question

$
0
0

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” “

Wscript.Echo strPhrase

When executed, it throws: Error 800A0401 Microsoft VBScript compilation error: Expected end of statement Microsoft VBScript compilation error

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"" "

Wscript.Echo strPhrase

VBScript - Sccessfully Escaped The Quote

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 ""  "

Wscript.Echo strPhrase

 

image

 

VBScript allowed two delimiters: the single quotation mark (') and the REM statement.  Both examples are highlighted below.

image

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'

Using Different Quotes In PowerShell

Notice the difference there?  Look at the underlined items in the below, and compare:

Using Different Quotes In PowerShell - Note The Underlined Differences

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:\Scripts\oldskool.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. 

Legacy CMD Escape Fun - From Way Back Then!

We need to escape the % symbol.  The command when placed into a batch file must look like this:

FOR %%f IN (C:\Scripts\oldskool.vbs) DO @ ECHO %%f

Updated Batch File Contents - Note The Double %%

And then it runs as expected.

Batch File - Command Now Sucessfully Escaped

 

 

Cheers,

Rhoderick


Viewing all articles
Browse latest Browse all 34890

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>