• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Tutorials / PowerShell Quotes

PowerShell Quotes

11/05/2018 by Stephanos 1 Comment

PowerShell Quotes

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.

Code:

$Date = Get-Date -Format d
$Msg = 'Today is $Date'

Output:

Single Quotes with Variable

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.

Code:

$Date = Get-Date -Format d
$Msg = "Today is $Date"

Output:

Double Quotes with Variable

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 $Date.

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

Code:

$Date = Get-Date -Format d
$Msg = "The value of `$Date is $Date"

Output:

String Variable Twice

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.

[adinserter name=”In Article”]

In the next examples we will see how we can integrate single or double quotation marks in our strings.

Code:

$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"

Output:

Quotes in 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.

Code:

$Msg = "I'm really happy today"
$Msg = 'I''m really happy today'

Output:

Short words

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.

Code:

$Msg = "
Hello World.
This is a new day.
Today is sunny.
"
$Msg = '
Hello World.
This is a new day.
Today is sunny.
'

[adinserter name=”In Article”]

Here-Strings

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.

Code:

$Msg = @"
Hello World.
This is a new day.
Today is sunny.
"@
$Msg = @'
Hello World.
This is a new day.
Today is sunny.
'@

Output:

Multi strings

On our first example for here-string we will see that double quotes and single quotes have the same effect on variables.

Code:

$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.
'@

Output:

Multi strings with Variable

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.

Options:

  • 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

Code:

$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.
'@

Output:

Multi string Double in Double

Multi String Single in Double

Multi string Double in Single

Multi String Single in Single

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.

[adinserter name=”In Article”]

I hope the tutorial about PowerShell Quotes is helpful.

Please let me know your comments and thoughts. You feedback is appreciated.

Related Links:

  • PowerShell Tutorials
  • PowerShell Scripts
  • about_Quoting_Rules | Microsoft Docs

[adinserter name=”Matched-Content”]

Summary
PowerShell Quotes
Article Name
PowerShell Quotes
Description
PowerShell Quotes. In this tutorial you will learn how quotes in PowerShell can affect the output for your stings. Stephanos Constantinou Blog - PowerShell Scripting
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Tutorials Tagged With: PowerShell Quotes

Reader Interactions

Trackbacks

  1. PowerShell Quotes - How to Code .NET says:
    11/05/2018 at 20:00

    […] on May 10, 2018by admin submitted by /u/SConstantinou [link] [comments] No comments […]

    Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact