The Difference Between Single and Double Quotes in PowerShell

Photo by Luca Bravo on Unsplash

“Quote me as saying I was mis-quoted.” -Groucho Marx

There are two types of quotes that can be used in PowerShell. Single and Double quotations. Some critical differences between the two can make or break a script. Knowing these differences will make you a more effective PowerShell scriptwriter and help you avoid a rather simple mistake.

In this post, I’ll quickly explain these differences and provide examples of each scenario.

‘Single Quotation’

Single quotation strings are what you will most often use and encounter when creating or troubleshooting PowerShell scripts.

Consider the following example:

Are you an IT pro ready to take your career to the next level? If so, join our contributor program! You don't have to be an expert presenter, a Microsoft MVP or even have a blog. We want your knowledge! You will be coached on presentation skills, become a member of our awesome community and get paid some nice side hustle income every, single month! Sign up today!.

Now examine the output:

In the above case, PowerShell ignores  $MyVar1 and treats the variable literally as $MyVar1, exactly what was typed. There is no substitution here.

But how do you get PowerShell to recognize the variable value within a quoted string value? That’s where double quotation comes in.

“Double Quotation”

Double quotation gives you a dynamic element to string values. You will encounter this type of string quotation when the string contains dynamic data from variables stored in memory or dynamically generated.

Consider the following example:

Now examine the output:

In the above case, PowerShell processes $MyVar2 because it was enclosed by a double-quoted string. Double quotes make PowerShell parse for text (the variable) preceded by a dollar sign and substitutes the variable name the corresponding value.

Real World Scenario

Now, apply this knowledge to a real scenario. Let’s say that you need to create a small function that will give an operator on your team some real basic information:

  • Date / Time
  • Disk % Used
  • Disk % free

You need to return this information visually to an operator. Simple.

First, some pseudo code. We need to display the date time as today’s date and time. Think about how this string value will work. We can use Get-Date and the -Uformat  parameter to give us the required date/time by using the correct patterns:

Testing the code in a PowerShell terminal confirms this works:

That takes care of the first part of the script. Now, I need to gather some disk information to also output to the terminal. The key metric I’m looking for is the percentage of free space remaining.  I’ll display this information using Write-Host again, but this time I’ll need to insert additional code inside the double-quoted string. Remember, this information will be dynamic. For the purposes of this example, I’m going to create a variable, then utilize an available member type property to get the value I’m looking for:

Testing the code in a PowerShell terminal confirms this works:

Perfect. We now have two variables that we can place in the strings that the operator will see when running this function. So let’s assemble the bits into the final script that will become our function:

Testing again in a PowerShell terminal, here is what the operator would see:

Notice what I did inside the last Write-Host line with the $disk variable. PowerShell evaluates the $( ) construct as an entire subexpression then replaces the result. Doing it this way also helps you avoid having to create more variables, which saves memory and can even make your script faster.

The function still needs some work. So let’s finish it off by adding some math to show a full calculation to the operator:


The operator can now make some faster decisions while supporting a remote system by using this function.

Final Thoughts

There’s not much to quotes in PowerShell. The one key concept to remember is that you need to know when to be literal ( ‘ ‘ ), and when to be dynamic ( ” ” ). By default, you should always use single quotes unless there is a requirement for dynamic data in the string construct. I hope you found this information useful!

Additional Resources

To learn more about quotation rules, visit the about_Quoting_Rules PowerShell documentation from Microsoft or this excellent MSDN article.
For even more examples of single/double quote usage, read Kevin Marquette’s “Everything you wanted to know about variable substitution in strings” .

One Reply to “The Difference Between Single and Double Quotes in PowerShell”

Leave a Reply

Your email address will not be published. Required fields are marked *