In today’s tutorial, I will explain how to handle variables within quotes in PowerShell. I will explain how to use PowerShell variables in quotes, including its syntax, and with some examples.
PowerShell variables within quotes is tricky. Single quotes (') treat the content literally, meaning variables inside them are not expanded. For instance, 'Hello, $name' outputs Hello, $name. Double quotes (") allow for variable expansion, so "Hello, $name" would output Hello, Jane Smith if $name = "Jane Smith".
Now, let us understand from the basics.
In PowerShell, variables store data that can be referenced and manipulated throughout your scripts. A variable is a container that holds information. In PowerShell, variables are prefixed with a dollar sign ($) and can store different data types, such as strings, integers, arrays, and objects.
To declare a variable in PowerShell, you assign a value to it using the = operator. Here’s an example:
$name = "Jane Smith"
$age = 35Use PowerShell Variables in Quotes
When it comes to using variables within quotes, in PowerShell, you can use both single quotes (‘) and double quotes (“). Now, let me show you the difference between these two types of quotes while using PowerShell variables.
Single Quotes (')
Single quotes in PowerShell are used to create literal strings. This means that the content within single quotes is taken exactly as it is, without any variable expansion.
Let me show you an example.
Example
$name = "Jane Smith"
$message = 'Hello, $name'
Write-Output $messageOutput:
Hello, $nameIn this example, the variable $name is not expanded because single quotes treat the string literally.
You can see the exact output in the screenshot below:

Check out PowerShell Single vs Double Quotes
Double Quotes (")
Double quotes, on the other hand, allow for variable expansion. This means that any variable within double quotes will be evaluated, and its value will be inserted into the string.
Here is an example.
Example
$name = "Jane Smith"
$message = "Hello, $name"
Write-Output $messageOutput:
Hello, Jane SmithHere, the variable $name is expanded, and its value is included in the output string.
I executed the above script using VS code, and you can see the exact output in the screenshot below:

Check out PowerShell Variable Naming Conventions
PowerShell Variables in Quotes Examples
Now, let me show you some scenarios and examples of using quotes in PowerShell variables.
Scenario 1: Including Quotes in Strings
Sometimes, you might need to include quotes within your strings. To do this, you can use escape characters or alternate between single and double quotes in PowerShell.
Example with Escape Characters
Here is a small example to help you understand it better. In PowerShell, you can use double double-quotes ("") to escape double quotes within a string enclosed by double quotes.
$message = "She said, ""Welcome to New York!"""
Write-Output $messageOutput:
She said, "Welcome to New York!"You can see the output in the screenshot below:

Example with Alternate Quotes
$message = 'She said, "Welcome to New York!"'
Write-Output $messageOutput:
She said, "Welcome to New York!"Here is the exact output in the screenshot below:

Check out Escape Single Quotes in PowerShell
Scenario 2: Using Variables Within JSON Strings
When working with JSON strings, you often need to include variables within the JSON structure. Using double quotes is essential in this case.
Example
Here is an example and the PowerShell script.
$name = "Jane Smith"
$json = "{ \"name\": \"$name\" }"
Write-Output $jsonOutput:
{ "name": "Jane Smith" }Scenario 3: Passing Variables to Commands
When passing variables to commands or functions in PowerShell, using double quotes ensures that the variables are expanded correctly.
Example
$fileName = "summary.txt"
Start-Process -FilePath "notepad.exe" -ArgumentList "$fileName"This command will open the file summary.txt in Notepad.
Scenario 4: Combining Variables and Strings
You might need to combine multiple variables and strings in a single output in PowerShell. Using double quotes makes this straightforward.
Here is an example.
Example
$firstName = "Jane"
$lastName = "Smith"
$message = "Full Name: $firstName $lastName"
Write-Output $messageOutput:
Full Name: Jane SmithHere is also the exact output in the screenshot below:

Conclusion
In this tutorial, I explained how to use PowerShell variables within quotes, focusing on the differences between single quotes and double quotes. We also saw some useful scenarios, such as including quotes in strings, using variables within JSON strings, passing variables to commands, and combining multiple variables and strings.
You may also like:
- How to Get the Type of a Variable in PowerShell?
- How to Prompt for Variable in PowerShell?
- PowerShell New-Variable Cmdlet
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.