In this tutorial we will talk about PowerShell quotes. Quotations marks are used to enclose our strings. We can use single quotation mark ( ‘ ) or double quotation mark ( ” ). Each one of them has its own significance. Single quotes, in general, are showing the string enclosed in it exactly as it is. Double quotes are first evaluating any variable, cmdlet or expression before the string is presented. Below we will see few examples for different conditions and how we will be able to achieve whatever we want to present. The examples are simple but the will help you understand how PowerShell quotes work.
Single line strings
In our first examples we will see how single and double quotes present the same string. We are using a variable that we have defined to be the current date by using the below:
$Date = Get-Date -Format d
In our first example we will use single quotes.
$Date = Get-Date -Format d $Msg = 'Today is $Date'
As you can see above the variable is not evaluated and is shown exactly as it is in the string. When we called the
$Msg variable the string is presented exactly as we have defined it.
On our next example we are using double quotes. We will use the same code and see the difference that we have in our sting output.
$Date = Get-Date -Format d $Msg = "Today is $Date"
As you can see above, $Date variable has been evaluated before the string that we have defined in our
$Msg variable. The result of this, is to show the actual value of
In the next example we will see the a string that contains
$Date variable twice. In this example the first time we present the variable name and the second time we present the actual value of the
$Date = Get-Date -Format d $Msg = "The value of `$Date is $Date"
As you can see above in order to be able to show a variable by the name we need to use back tick in order no to evaluate the dollar ( $ ) sign. The second time that the variable is in the string we need the value, so we will not use anything. Using this method will provide us with results like the above.
In the next examples we will see how we can integrate single or double quotation marks in our strings.
$Msg = 'I want to enclose "Hello World" in double quotes' $Msg = "I want to enclose ""Hello World"" in double quotes" $Msg = "I want to enclose 'Hello World' in single quotes"
As you can see above in the output there are three different ways to include quotation marks within our strings. It depends what you want to achieve you will need to use the correct way to achieve it.
There are some cases that we want to use contractions in our strings. Below you will see how you are able to achieve this by using single or double quotation marks.
$Msg = "I'm really happy today" $Msg = 'I''m really happy today'
As you can see from the above output, If you are using double quotation marks, you are able to write the string normally. If you will use single quotation marks, you need to enter another single quotation mark just before the actual single mark.
Multi line strings
As you may know, you are able to have multi-line strings. If you are using multi-line stings then the same rules as above are applied. You will have to use the same principles in order to use quotation marks within the multi-line strings.
A multi-line string can use any quotation mark to enclose the string. Below are some example of multi-line strings. I will not provide more example on how quotation marks are reacting as the same rules apply.
$Msg = " Hello World. This is a new day. Today is sunny. " $Msg = ' Hello World. This is a new day. Today is sunny. '
The below explanation of what is a here string is provided directly from Microsoft. After the explanation of what is a here string we will see some examples that will help you understand.
A here-string is a single-quoted or double-quoted string in which quotation marks are interpreted literally. A here-string can span multiple lines. All the lines in a here-string are interpreted as strings even though they are not enclosed in quotation marks.
Like regular strings, variables are replaced by their values in double-quoted here-strings. In single-quoted here-strings, variables are not replaced by their values.
You can use here-strings for any text, but they are particularly useful for the following kinds of text:
- Text that contains literal quotation marks
- Multiple lines of text, such as the text in an HTML or XML
- The Help text for a script or function document
The below examples are related to here-strings and how you are able to achieve what you want with the quotation marks.
$Msg = @" Hello World. This is a new day. Today is sunny. "@ $Msg = @' Hello World. This is a new day. Today is sunny. '@
On our first example for here-string we will see that double quotes and single quotes have the same effect on variables.
$Msg = @" Hello World. This is a new day. Today is $Date Today is sunny. "@ $Msg = @' Hello World. This is a new day. Today is $Date Today is sunny. '@
As you can see above, The value of
$Date is not populated when we are using single quotation marks.
The next example is about using quotation marks within the here-strings. At this point PowerShell acts differently than in single or multi line strings. As you will see below in the code, we are using all options of having quotation marks within the string.
- Double quotation marks within double quotation marks
- Single quotation marks within double quotation marks
- Double quotation marks within single quotation marks
- Single quotation marks within single quotation marks
$Msg = @" Hello World. This is a "new" day. Today is sunny. "@ $Msg = @" Hello World. This is a 'new' day. Today is sunny. "@ $Msg = @' Hello World. This is a "new" day. Today is sunny. '@ $Msg = @' Hello World. This is a 'new' day. Today is sunny. '@
As you can see there are several ways to use the single or double quotation marks. This depends on what you want to achieve with your strings and how you want to be presented. The best way is to practise with them and learn how they react.
I hope the tutorial about PowerShell Quotes is helpful.
Please let me know your comments and thoughts. You feedback is appreciated.