Windows PowerShell Quotes

Introduction to Windows PowerShell Quotes

Did you ever use a traditional single-bladed screwdriver on a square ‘Philips’ head?  Sometimes you get away with it, but sooner or later you will literally "screw up" – well it’s the same with PowerShell’s quotes.

Topics for PowerShell Quotes

 ♣

Introduction to the right sort of PowerShell quotation mark

In Windows PowerShell, there are two types of quotes ‘single speech marks’ and "double speech marks"; each has a particular meaning and significance.  The first point is that merely because you use a string, PowerShell does not require any sort of quote.  That said, there are times when you will need not only quotes, but also the right sort of quote, for example:

# PowerShell quotes needed
Set-Location c:\ documents and settings. 

The above command draws an error message, something about a parameter called and.  Fortunately, quotes will solve the problem thus:

Set-Location "c:\ documents and settings"
# or
Set-Location ‘c:\ documents and settings’

Either single or double quotes work in this instance, and you may deduce that they will always be interchangeable – wrong.  Take the time to understand the different role for ‘single quotes’ and "double quotes".

Example 1 PowerShell ‘Single Quote’

Call me paranoid, but when I say ‘Single Quote’, I am talking about the key sandwiched between the semi-colon and the hash # key.   This key is also known as the Apostrophe and corresponds to ASCII 039 in decimal.

# PowerShell Single Quotes
$Days = 7
Write-Host ‘There are $Days in a week’

We have a problem, the above command does not expand the variable $Days into the number 7.  What happens instead is we get the literal: There are $Days in a week.  Let us see what happens with double quotes in example 2.

Example 2 PowerShell "Double Quotes"

Double quotes are required in situations where you need to expand variables.  This is also known as variable substitution, and it only happens if you employ double quotes.

# Note: "Double quotes for $Days"
$Days = 7
Write-Host "There are $Days in a week"

In Example 2 PowerShell expands the command, it switches to what I call mega clever mode, and realizes that I intended $Days to be a variable and not a literal word.

SolarWinds Firewall Browser Solarwinds Free Firewall Browser

Here is an utility where you can review firewall settings such as access control lists (ACL), or troubleshoot problems with network address translation (NAT).

Other reasons to download this SolarWinds Firewall Browser include managing requests to change your firewall settings, testing firewall rules before you go live, and querying settings with the browser’s powerful search options.

Guy recommends that you download a copy of the SolarWinds free Firewall Browser.

Keyboards and the Quote Keys

While the "double quote" is a distinctive symbol, its location on the keyboard varies according to your country.  However, if you are in doubt then check what I mean by holding down the left hand Alt key, and typing 034 on the numeric keyboard. 

hat makes me twitchy about the precise definition of the single and double quotes is the memory of spending 3 days troubleshooting a script where a ‘Psycho’ used two single quotes instead of one double quotes.  It took the eyes of a hawk to distinguish between "Results" and ”Results”.

Summary of PowerShell Quotes

PowerShell employs both single and double quotes, however they have different purposes.  Double quotes expand the enclosed string, whereas single quotes treat the string as a literal.  What ever you do, avoid the trap of two single quotes (ASCII decimal 039) when you need a double quote (ASCII decimal 034).

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials

PowerShell Tutorials  • Methods  • Cmdlets  • PS Snapin  • Profile.ps1  • Exchange 2007

Command & Expression Mode  • PowerShell pipeline (|)  • PowerShell ‘where‘  • PowerShell ‘Sort’

Windows PowerShell Modules  • Import-Module  • PowerShell Module Directory 

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links.