Q. What are the differences between single and double quotes in PowerShell?

A. There’s only one difference between single and double quotes in PowerShell: Inside double quotes, PowerShell looks for the $ character. If it finds it, it assumes that any following characters, up to the next white space, are a variable name. It replaces the variable reference with the variable’s contents. Consider this:

$a = "Hello"
$b = "$a World"

The $b variable will now contain "Hello World" because $a will be replaced with its contents, “Hello.” Single quotes don’t do this:

$a = 'Hello'
$b = '$a World'

The $b variable now contains "$a World"—the same literal characters we put into it. As a best practice, try to only use double quotation marks when you need this variable-replacement feature; doing so will keep the feature from kicking in when you’re not expecting it.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish