Single Quotes vs. Double Quotes in PowerShell: What's the Difference?

I received variations on this question from no less than four folks today, so it's the official Question of the Day: What's the difference between single and double quotes in PowerShell? Which should I use?

Both kinds of quotes are used to delimit string values. In general, you should use single quotes:

Write-Host 'This is a message'

There are three instances when you might want to shift to double quotes. First, when you want to have variable names replaced with their contents:

$var = 'World'
$out = "Hello $var"
# $out now contains 'Hello World'

That's a useful trick, since it saves you from having to concatenate strings in a lot of situations. Second, when you need to delimit a string within the string - such as in a SQL query:

$query = "SELECT * FROM Customers WHERE Name LIKE '%JONES%'"

Using outer double quotes lets you use the single quotes needed within the string. Finally, when you need to use an escape character, since those aren't parsed within single quotes:

Get-Process | Export-CSV processes.tdf -delimiter "`t"

That creates a tab-delimited file. Apart from those three instances, it's generally considered a best practice to stick with single quotes.



Training Update: SAPIEN Technologies is having me teach a four-day Windows PowerShell class, using the courseware I designed for them, in Las Vegas this February. It'll be at the freshly-remodeled Tropicana hotel (if you haven't seen it in the past couple of months, it's really gone from woesome to awesome). Here are the details. If that's a bit soon for you, I'll have my own 5-day PowerShell Retreat in June, also in Vegas (although slightly off-Strip). Discounted pricing for that is still available, which includes most of your meals and your hotel room for under $3000! It'll be co-located with Greg Shields' Windows 7 Deployment Retreat. Hope to see you in a class soon!

Discuss this Blog Entry 2

on Dec 10, 2010
Why are single quotes considered best practice?
on Dec 10, 2010
Why are single quotes generally considered best practice?

Please or Register to post comments.

What's PowerShell with a Purpose Blog?

Don Jones demystifies Windows PowerShell.

Blog Archive

Sponsored Introduction Continue on to (or wait seconds) ×