4

Can anyone tell me what difference, if any, there is between the following PowerShell commands:

Set-ItemProperty -Path "$($var)" -Name $var2 -Value $var3

and

Set-ItemProperty -Path ("$var") -Name $var2 -Value $var3

$var is a registry location, i.e. HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion...

I have also seen both used for the Path parameter in New-ItemProperty

FrinkTheBrave
  • 3,894
  • 10
  • 46
  • 55
  • Possible duplicate of [What does $($variableName) mean in expandable strings in PowerShell?](https://stackoverflow.com/questions/13615676/what-does-variablename-mean-in-expandable-strings-in-powershell) – Lance U. Matthews Oct 17 '19 at 17:24
  • I think it is a different question because of the "s, but that other thread has interesting information too – FrinkTheBrave Oct 17 '19 at 19:46

1 Answers1

9

"$($var)" outputs the value of the variable $var in a subexpression and then puts that output in a string. The string is then used as an argument in the Set-ItemProperty statement.

("$var") puts the value of the variable $var in a string and evaluates that in a grouping expression (similar to subexpression, but doesn't allow multiple statements). The output from the grouping expression (which in this case is the string) is then passed as an argument in the Set-ItemProperty statement.

Neither is required in your example statement. Using the variable by itself is sufficent.

Set-ItemProperty -Path $var -Name $var2 -Value $var3

You'd use a subexpression ($(...)) in a string if you want to insert something other than a simple variable into a string that has other text as well, e.g. the output of another statement:

"foo $(Get-Date) bar"

the value of an object property:

"foo $($var.Foo) bar"

or an element of an array:

"foo $($var[5]) bar"

because these cannot be used directly in a string otherwise. If you need to insert the value of a variable into a string you can do that without a subexpression:

"foo ${var} bar"

You'd use a grouping expression ((...)) if you want the output of an expression used as the argument of the statement:

Do-Something -Foo (Get-Date)

Putting just a string into a subexpression doesn't make any sense.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Also lesser known, combining multiple statements. `$(echo hi; echo there) | measure` – js2010 Oct 17 '19 at 17:14
  • Thanks Ansgar, a very thorough answer. If $var contains spaces, then won't I need "$var" instead? – FrinkTheBrave Oct 17 '19 at 19:42
  • Running an external command, e.g. via the call operator, probably will require putting a variable in quotes. Passing variables to PowerShell cmdlets normally doesn't. – Ansgar Wiechers Oct 17 '19 at 23:21
  • @FrinkTheBrave: Even when calling external programs you don't need double quotes around variables: PowerShell stringifies the variable values anyway and rebuilds the command line behind the scenes, where it uses double quoting around the resulting string value _if needed_ (due to value-internal spaces). However, when you call external programs there are edge cases when you need to use _escaped_ quotes - see https://stackoverflow.com/a/51773694/45375 - and, sadly, you must additionally `\ `-escape _embedded_ `"` chars - see https://github.com/MicrosoftDocs/PowerShell-Docs/issues/2361. – mklement0 Oct 18 '19 at 04:02