Dec 04, 2013
Quoting in PowerShell
and embedding strings of text or evaluating expressions and variables in cmdlets are one of the main things that tend to trip up most newbies. Windows PowerShell was first released to the world around the time of Microsoft Windows Server 2008 and was designed from the outset as the next generation platform for administrative automation. If you want to become a server administrator these days, you do need to become familiar with PowerShell.
PowerShell is a very versatile and powerful administrative scripting language that can do a lot more in one line than previous systems such as MS-DOS Command shell, Windows Scripting Host (WSH), and jScript; and more than VBScript could ever do in many paragraphs of code. Even though it is generally very forgiving about things such as spaces, PowerShell still has a clearly defined syntax.
Before understanding quoting in PowerShell, we need to be clear that there are two quote characters that can be used, and they are both subtly different:
- The single quote (the ‘ ' character (ASCII 39 in decimal), generally to the left of the Enter key on your keyboard). This is referred to as a literal quote because it takes the contents of your enclosed string in literal form
- The second quote type is the double quote (the “ " character (ASCII 34 in decimal), again generally found to the left of the Enter key, but this time used in conjunction with the Shift key). This quote is referred to as an expanding string quote mark (i.e. it can “expand” or evaluate expressions and variables in the string).
These quote marks are used in pairs to signify the beginning and the end of a string. Although they can often be used interchangeably, they should generally not be mixed, or they will give errors. However, I will shortly show you some exceptions to this rule while quoting in PowerShell. For example, say we want to display the simple phrase The World is Round
on the screen, I can use either quote mark as follows (I use the alias Echo as a shortcut – aliases are great in PowerShell!):
Echo ‘The World is Round’
Echo “The World is Round”
Both of the above will give the same output as picture below.
However, if I wanted to evaluate a variable within a string, as written in the quotes below, the results would be quite different.
$Days = 7
Echo ‘There are $Days in the week’
Echo “There are $Days in the week”
See how the first example takes the string literally, but the second example evaluates the enclosed variable. Now, that's cool!
I did say earlier that we should not mix the quote marks, but we can combine them in some very interesting ways. Let’s say I wanted to include the double quotes within my string like this:
Echo “My name is “Gordon””
You would think the above example makes logical sense, but it does give a strange output!
However, if I combine the two types of quote marks in the following way, it produces a more readable display.
Echo ‘My Name is “Gordon”’
So there you go, a couple of very useful tips for quoting in PowerShell. Now it's your turn to try out your own variations!