PowerShell Variables in Quotes

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 = 35

Use 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 $message

Output:

Hello, $name

In 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:

powershell variable in quotes

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

Output:

Hello, Jane Smith

Here, 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:

PowerShell Variables in Quotes

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

Output:

She said, "Welcome to New York!"

You can see the output in the screenshot below:

powershell escape single quote in variable

Example with Alternate Quotes

$message = 'She said, "Welcome to New York!"'
Write-Output $message

Output:

She said, "Welcome to New York!"

Here is the exact output in the screenshot below:

powershell escape variable in quotes

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

Output:

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

Output:

Full Name: Jane Smith

Here is also the exact output in the screenshot below:

powershell escape single quote in variables

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.