tl;dr

Inside "...":

  • $(...) is only needed to embed entire expressions or commands (e.g. "PSVersionTable.PSVersion)")

  • ${...} is only needed if a stand-alone reference to a variable with a non-special name needs delimiting (disambiguation), so that subsequent characters in the string aren't interpreted as part of the variable name (e.g, "${foo}_bar" correctly embeds variable $foo; without {...}, PowerShell would look for variable $foo_bar; notably, a subsequent : requires this technique too: "${foo}:bar"; by contrast, something like "$foo!" also works without {...})

Independently of use in "...", ${...} is also needed for variable names containing special characters (e.g. ${a-b} = 42)

For a comprehensive overview of PowerShell's expandable strings (string interpolation), see this answer.


Background information:

To complement marsze's helpful answer:

${...} (enclosing the variable name in { and }) is indeed always necessary if a variable name contains special characters, such as spaces, . or -.

  • Not special are _ and - surprisingly and problematically - ?.

    • For the exact rules, see the relevant section of the conceptual "about_Variables" help topic, Variable Names that Include Special Characters.
  • (The first occurrence of) : in a name is invariably interpreted as terminating either a PowerShell drive reference - in the context of namespace variable notation - or a scope specifier, irrespective of whether {...} enclosure is used or required (e.g., in $env:USERNAME or ${env:USERNAME}, env refers to the Env: PowerShell drive representing all environment variables; in $script:foo or ${script:foo}, script refers to the script's scope and its variables).

Note:

  • ${...} - the syntax for disambiguating a variable name - is not to be confused with $(...), the subexpression operator, which is needed to embed any expression or command that goes beyond a stand-alone variable reference inside "...", an expandable (interpolating) string.

  • As such, these two syntax forms are independent of one another and may need to be combined in a given situation; e.g. "$var" / "${var}" work fine, but "$var.someProperty" / "${var}.someProperty" do not: you need "var.someProperty)" / "{var}.someProperty)"

In the context of "...", there is another reason to use ${...}, even if the variable name itself doesn't need it:

If you need to delineate the variable name from directly following non-whitespace characters, notably including ::

$foo = 'bar'  # example variable

# INCORRECT: PowerShell assumes that the variable name is 'foobarian', not 'foo'
PS> "A $foobarian."
A .  # Variable $foobarian doesn't exist -> reference expanded to empty string.

# CORRECT: Use {...} to delineate the variable name:
PS> "A ${foo}barian."
A barbarian.

# INCORRECT: PowerShell assumes that 'foo:' is a *namespace* (drive) reference
#            (such as 'env:' in $env:PATH) and FAILS:
PS> "$foo: bar"
Variable reference is not valid. ':' was not followed by a valid variable name character. 
Consider using ${} to delimit the name.

# CORRECT: Use {...} to delineate the variable name:
PS> "${foo}: bar"
bar: bar

See this answer for a comprehensive overview of PowerShell string-expansion rules.

Note that you need the same technique when string expansion is implicitly applied, in the context of passing an unquoted argument to a command; e.g.:

# INCORRECT: The argument is treated as if it were enclosed in "...",
#            so the same rules apply.
Write-Output $foo:/bar

# CORRECT
Write-Output ${foo}:/bar

Finally, a somewhat obscure alternative is to `-escape the first character after the variable name, but the problem is that this only works as expected with characters that aren't part of escape sequences (see about_Special_Characters):

# OK: because `: is not an escape sequence.
PS> "$foo`: bar"
bar: bar

# NOT OK, because `b is the escape sequence for a backspace character.
PS> "$foo`bar"
baar # The `b "ate" the trailing 'r' of the variable value
     # and only "ar" was the literal part.
Answer from mklement0 on Stack Overflow
Top answer
1 of 3
200

tl;dr

Inside "...":

  • $(...) is only needed to embed entire expressions or commands (e.g. "PSVersionTable.PSVersion)")

  • ${...} is only needed if a stand-alone reference to a variable with a non-special name needs delimiting (disambiguation), so that subsequent characters in the string aren't interpreted as part of the variable name (e.g, "${foo}_bar" correctly embeds variable $foo; without {...}, PowerShell would look for variable $foo_bar; notably, a subsequent : requires this technique too: "${foo}:bar"; by contrast, something like "$foo!" also works without {...})

Independently of use in "...", ${...} is also needed for variable names containing special characters (e.g. ${a-b} = 42)

For a comprehensive overview of PowerShell's expandable strings (string interpolation), see this answer.


Background information:

To complement marsze's helpful answer:

${...} (enclosing the variable name in { and }) is indeed always necessary if a variable name contains special characters, such as spaces, . or -.

  • Not special are _ and - surprisingly and problematically - ?.

    • For the exact rules, see the relevant section of the conceptual "about_Variables" help topic, Variable Names that Include Special Characters.
  • (The first occurrence of) : in a name is invariably interpreted as terminating either a PowerShell drive reference - in the context of namespace variable notation - or a scope specifier, irrespective of whether {...} enclosure is used or required (e.g., in $env:USERNAME or ${env:USERNAME}, env refers to the Env: PowerShell drive representing all environment variables; in $script:foo or ${script:foo}, script refers to the script's scope and its variables).

Note:

  • ${...} - the syntax for disambiguating a variable name - is not to be confused with $(...), the subexpression operator, which is needed to embed any expression or command that goes beyond a stand-alone variable reference inside "...", an expandable (interpolating) string.

  • As such, these two syntax forms are independent of one another and may need to be combined in a given situation; e.g. "$var" / "${var}" work fine, but "$var.someProperty" / "${var}.someProperty" do not: you need "var.someProperty)" / "{var}.someProperty)"

In the context of "...", there is another reason to use ${...}, even if the variable name itself doesn't need it:

If you need to delineate the variable name from directly following non-whitespace characters, notably including ::

$foo = 'bar'  # example variable

# INCORRECT: PowerShell assumes that the variable name is 'foobarian', not 'foo'
PS> "A $foobarian."
A .  # Variable $foobarian doesn't exist -> reference expanded to empty string.

# CORRECT: Use {...} to delineate the variable name:
PS> "A ${foo}barian."
A barbarian.

# INCORRECT: PowerShell assumes that 'foo:' is a *namespace* (drive) reference
#            (such as 'env:' in $env:PATH) and FAILS:
PS> "$foo: bar"
Variable reference is not valid. ':' was not followed by a valid variable name character. 
Consider using ${} to delimit the name.

# CORRECT: Use {...} to delineate the variable name:
PS> "${foo}: bar"
bar: bar

See this answer for a comprehensive overview of PowerShell string-expansion rules.

Note that you need the same technique when string expansion is implicitly applied, in the context of passing an unquoted argument to a command; e.g.:

# INCORRECT: The argument is treated as if it were enclosed in "...",
#            so the same rules apply.
Write-Output $foo:/bar

# CORRECT
Write-Output ${foo}:/bar

Finally, a somewhat obscure alternative is to `-escape the first character after the variable name, but the problem is that this only works as expected with characters that aren't part of escape sequences (see about_Special_Characters):

# OK: because `: is not an escape sequence.
PS> "$foo`: bar"
bar: bar

# NOT OK, because `b is the escape sequence for a backspace character.
PS> "$foo`bar"
baar # The `b "ate" the trailing 'r' of the variable value
     # and only "ar" was the literal part.
2 of 3
35

Note that $() is helpful for json objects:

"My json property is jsonObj.property)"
🌐
Delft Stack
delftstack.com › home › howto › powershell › string interpolation in powershell
String Interpolation in PowerShell | Delft Stack
December 14, 2023 - Python ScipyPythonPython ... interpolation in PowerShell is a powerful feature that facilitates dynamic string formatting by embedding variable values, expressions, and commands within strings....
Discussions

Support formatting specifiers in string interpolation
Summary of the new feature/enhancement It would be a great adddition to Powershell to support full-blown string interpolation, i.e. including the ability to specify the format of the expanded token... More on github.com
🌐 github.com
3
April 5, 2020
Add syntax for a fast-fail/strict expansion to string interpolation
Summary of the new feature / enhancement As a user, I frequently use environment variables to refer to (for example) certain filesystem locations. Many times, the commands I use are pasted into the command-line from a file of snippets, r... More on github.com
🌐 github.com
14
August 27, 2024
Support Named Token String Replacement
As a PowerShell developer, I want to allow variable name interpolation, to improve readability and maintainability of strings used as a template. ... Right now we can do this via the example above, which means if you begin to pass in 5-6 variables, you have to maintain the 0 index position for them carefully. More intuitive text similar to python... More on github.com
🌐 github.com
12
December 20, 2019
Come up with a better concat/string interpolation strategy
Today in PSArm, you must use the concat function to make string concatenation work at template deployment time. Ordinary PowerShell string manipulation methods work if all your inputs are string li... More on github.com
🌐 github.com
4
April 2, 2021
🌐
Reddit
reddit.com › r/powershell › enhanced string interpolation
r/PowerShell on Reddit: Enhanced String Interpolation
October 7, 2020 -

Hi everyone,

I'm certain PowerShell has this feature, but I can't remember the name of it.

I'm looking for formatting string using interpolation but where the string is interpolated at the time of reading it rather than at creation. In doing so, it takes the current value of the included variables rather than being stuck on the past contents of the variables. I think Python's f strings are what I'm after, but I could be wrong.

Any ideas?

🌐
GitHub
github.com › PowerShell › PowerShell › issues › 12259
Support formatting specifiers in string interpolation · Issue #12259 · PowerShell/PowerShell
April 5, 2020 - It would be a great adddition to Powershell to support full-blown string interpolation, i.e. including the ability to specify the format of the expanded tokens. Essentially like C#'s https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated or Python's https://www.python.org/dev/peps/pep-0498.
Author   stinos
🌐
Adam the Automator
adamtheautomator.com › powershell-data-types-guide
PowerShell 101: String, Boolean, and Number Data Types
November 26, 2024 - Learn how to work with PowerShell's fundamental data types including strings, booleans and numbers. Master string interpolation, boolean logic, and numerical operations through practical examples.
🌐
GitHub
github.com › PowerShell › PowerShell › issues › 24218
Add syntax for a fast-fail/strict expansion to string interpolation · Issue #24218 · PowerShell/PowerShell
August 27, 2024 - At line:1 char:13 + Write-Host "${?env:b}" + ~~ + CategoryInfo : InvalidOperation: (b:String) [], RuntimeException + FullyQualifiedErrorId : VariableIsUndefined $x = "xyz321" Write-Host "$x" >> xyz321 Write-Host "${local:x}" >> xyz321 Write-Host "${?local:x}" >> xyz321 Write-Host "${?local:y}" >> The variable '$local:y' cannot be retrieved because it has not been set.
Author   jimbobmcgee
🌐
ShellGeek
shellgeek.com › home › powershell tips › string interpolation in powershell
String Interpolation in PowerShell - ShellGeek
April 14, 2024 - String Interpolation in PowerShell is the way of replacing the value of a variable into placeholders in a string. It displays variable value
Find elsewhere
🌐
Microsoft
devblogs.microsoft.com › dev blogs › powershell team › variable expansion in strings and here-strings
Variable expansion in strings and here-strings - PowerShell Team
February 18, 2019 - Cmds with > 800 handles are: CCMEXEC CSRSS IEXPLORE IEXPLORE LSASS OUTLOOK POWERSHELL PS SEARCHINDEXER SVCHOST SYSTEM WINLOGON · So by using $(), you can do almost anything you could want to do. ... Yeah – almost but not really everything. Notice that I used single quotes for the format string in the expression: ‘`n`t{0}’ .
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › powershell › how to use string interpolation in powershell?
How to Use String Interpolation in PowerShell? - SharePoint Diary
September 19, 2025 - Here’s a quick comparison to show how string interpolation makes things easier: ... # Without interpolation $name = "Smith" $message = "Hello, " + $name + "! Welcome to PowerShell." Write-Output $message # With interpolation $name = "Smith" $message = "Hello, $name!
🌐
Easy IT Tutorials
easyittutorials.wordpress.com › 2020 › 01 › 16 › powershell-string-concatenation-and-interpolation
PowerShell: String Concatenation and Interpolation – Easy IT Tutorials
January 16, 2020 - By using string interpolation, we can connect a set of strings and variables with only 1 set of quotation marks, and without the ‘+’ operator, which makes the whole string much easier to read and interpret. Let’s look at the same examples we used string concatenation above, and connect them using string interpolation:
🌐
GitHub
github.com › PowerShell › PowerShell › issues › 11412
Support Named Token String Replacement · Issue #11412 · PowerShell/PowerShell
December 20, 2019 - As a PowerShell developer, I want to allow variable name interpolation, to improve readability and maintainability of strings used as a template. ... Right now we can do this via the example above, which means if you begin to pass in 5-6 variables, you have to maintain the 0 index position for them carefully. More intuitive text similar to python...
Author   sheldonhull
🌐
GitHub
github.com › PowerShell › PSArm › issues › 144
Come up with a better concat/string interpolation strategy · Issue #144 · PowerShell/PSArm
April 2, 2021 - Moreover, we often need to embed non-variable expressions in the interpolated strings, for example: ... Output 'sshCommand' -Type 'string' -Value (concat 'ssh ' $adminUsername '@' (reference $publicIPAddressName).dnsSettings.fqdn) If we use a PowerShell string interpolation there:
Author   rjmholt
🌐
Powercmd
powercmd.com › home › powershell variables in strings: practical tips for using
PowerShell Variable in String: How to Master Variable Interpolation
October 4, 2023 - Subexpressions, enclosed within `$()`, empower you to execute complex expressions and include their outcomes in strings. For instance: ```powershell $quantity = 5 $pricePerUnit = 10 Write-Host "Total cost: $($quantity * $pricePerUnit)" ``` ... Consider scenarios where variables may be null or empty when interpolating them into strings.
🌐
Codeporting
products.codeporting.ai › convert › powershell-to-python
AI-Powered PowerShell to Python Snippet Converter
For more details, see the PowerShell Module Documentation and the Python Import System Documentation. PowerShell uses $variable for string interpolation, while Python uses f-strings or the .format() method.
🌐
Devopswarm
devopswarm.com › home › blog › interpolation in powershell
Interpolation in PowerShell - Devopswarm
August 2, 2024 - Interpolation allows you to expand the variables within the string type declarations and outputs during runtime. For example: Look at the code below for a number given by the user.
Top answer
1 of 3
200

tl;dr

Inside "...":

  • $(...) is only needed to embed entire expressions or commands (e.g. "$($PSVersionTable.PSVersion)")

  • ${...} is only needed if a stand-alone reference to a variable with a non-special name needs delimiting (disambiguation), so that subsequent characters in the string aren't interpreted as part of the variable name (e.g, "${foo}_bar" correctly embeds variable $foo; without {...}, PowerShell would look for variable $foo_bar; notably, a subsequent : requires this technique too: "${foo}:bar"; by contrast, something like "$foo!" also works without {...})

Independently of use in "...", ${...} is also needed for variable names containing special characters (e.g. ${a-b} = 42)

For a comprehensive overview of PowerShell's expandable strings (string interpolation), see this answer.


Background information:

To complement marsze's helpful answer:

${...} (enclosing the variable name in { and }) is indeed always necessary if a variable name contains special characters, such as spaces, . or -.

  • Not special are _ and - surprisingly and problematically - ?.

    • For the exact rules, see the relevant section of the conceptual "about_Variables" help topic, Variable Names that Include Special Characters.
  • (The first occurrence of) : in a name is invariably interpreted as terminating either a PowerShell drive reference - in the context of namespace variable notation - or a scope specifier, irrespective of whether {...} enclosure is used or required (e.g., in $env:USERNAME or ${env:USERNAME}, env refers to the Env: PowerShell drive representing all environment variables; in $script:foo or ${script:foo}, script refers to the script's scope and its variables).

Note:

  • ${...} - the syntax for disambiguating a variable name - is not to be confused with $(...), the subexpression operator, which is needed to embed any expression or command that goes beyond a stand-alone variable reference inside "...", an expandable (interpolating) string.

  • As such, these two syntax forms are independent of one another and may need to be combined in a given situation; e.g. "$var" / "${var}" work fine, but "$var.someProperty" / "${var}.someProperty" do not: you need "$($var.someProperty)" / "$(${var}.someProperty)"

In the context of "...", there is another reason to use ${...}, even if the variable name itself doesn't need it:

If you need to delineate the variable name from directly following non-whitespace characters, notably including ::

$foo = 'bar'  # example variable

# INCORRECT: PowerShell assumes that the variable name is 'foobarian', not 'foo'
PS> "A $foobarian."
A .  # Variable $foobarian doesn't exist -> reference expanded to empty string.

# CORRECT: Use {...} to delineate the variable name:
PS> "A ${foo}barian."
A barbarian.

# INCORRECT: PowerShell assumes that 'foo:' is a *namespace* (drive) reference
#            (such as 'env:' in $env:PATH) and FAILS:
PS> "$foo: bar"
Variable reference is not valid. ':' was not followed by a valid variable name character. 
Consider using ${} to delimit the name.

# CORRECT: Use {...} to delineate the variable name:
PS> "${foo}: bar"
bar: bar

See this answer for a comprehensive overview of PowerShell string-expansion rules.

Note that you need the same technique when string expansion is implicitly applied, in the context of passing an unquoted argument to a command; e.g.:

# INCORRECT: The argument is treated as if it were enclosed in "...",
#            so the same rules apply.
Write-Output $foo:/bar

# CORRECT
Write-Output ${foo}:/bar

Finally, a somewhat obscure alternative is to `-escape the first character after the variable name, but the problem is that this only works as expected with characters that aren't part of escape sequences (see about_Special_Characters):

# OK: because `: is not an escape sequence.
PS> "$foo`: bar"
bar: bar

# NOT OK, because `b is the escape sequence for a backspace character.
PS> "$foo`bar"
baar # The `b "ate" the trailing 'r' of the variable value
     # and only "ar" was the literal part.
2 of 3
35

Note that $() is helpful for json objects:

"My json property is $($jsonObj.property)"
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › powershell › powershell string › powershell string manipulation: a comprehensive guide
PowerShell String Manipulation: A Comprehensive Guide - SharePoint Diary
November 16, 2021 - The expressions inside the $() syntax will be evaluated before the string is output. This makes string interpolation a flexible and efficient tool for creating dynamic strings in your scripts. Another common task when working with strings in PowerShell is checking if a string contains a specific substring.
🌐
Adam the Automator
adamtheautomator.com › powershell-string-format
PowerShell String Formatting: Master the Basics
October 1, 2023 - Expanding variables inside of strings and using PowerShell’s string format capabilities, you can do just about anything.
🌐
FoxLearn
foxlearn.com › home › powershell › how to use string interpolation in powershell
How to use string interpolation in PowerShell
October 3, 2024 - String interpolation in PowerShell allows you to embed variables within strings, improving the readability and flexibility of your scripts.