PowerShell Best Practice #1 - Use Single Quotes

I'm going to kick off this series with a no-brainer.

In PowerShell, there are 2 ways to quote strings, using single-quotes (') or double-quotes ("). This is probably not a surprise to you if you've seen PowerShell scripts before.

A "best practice" in PowerShell is that you should always default to using single-quotes unless you specifically need to use double-quotes. The reasons to use double-quotes are:

  • To enable substitution in the string (variables or expressions)
  • To utilize escape sequences in the string (introduced by backtick `)
  • To simplify embedding single-quotes in the string (without doubling the single quotes)

I have to admit, I find myself getting lazy about this and switching between types of quotes with no rhyme or reason. In fact, sometimes I see that I'm using double-quotes as the default just in case I end up doing variable substitution. In my opinion, however, this is not something I should be doing.

Here's a post from Don Jones about quoting.

Anyone disagree with this one?