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:

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:

Results:

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” .

Installing PowerShell Core Everywhere

DevOps is requiring that SysAdmins be experts in more than one operating system. That used to mean learning more than a few shell scripting languages. PowerShell Core is changing that.

With PowerShell Core, it is no longer necessary to learn a new scripting language to support heterogeneous environments.

PowerShell Core is a new edition of PowerShell that is cross-platform (Windows, macOS, and Linux), open-source, and built for heterogeneous environments and the hybrid cloud.

It has recently become available on Windows Internet of Things (IoT). The cross-platform nature of PowerShell Core means that scripts that you write will run on any supported operating system.

What’s the Difference?

The main difference is the platforms they are built on.

Windows PowerShell is built on top of .NET FrameWork and  because of that dependency, is only available on Windows and is launched as powershell.exe

PowerShell Core is built on .NET Core and is available cross platform and is launched as pswh.exe

Installing PowerShell Core

To install on a Windows client or Windows Server, navigate to the GitHub repository – PowerShell Core – and download the .msi package appropriate for your system.

Windows IoT devices already have PowerShell installed which we will use for installing Powershell Core

For Linux Distributions, it just a matter of adding the repository and installing with the package manager.

For Ubuntu, Debian

For CentOS and RedHat

For OpenSUSE

and finally, for Fedora

 

For macOS, Homebrew is the preferred package manager.

Installing Homebrew package manager is a single line command from a terminal, then install Powershell Core.

Embracing DevOps means being able to manage different platforms and OS’s and learning different shell scripting programs maintain them.  With PowerShell Core, you write once, deploy everywhere. It’s another tool in your toolbox.

If you don’t learn it, someone else will.