$() is a subexpression operator. This means "first evaluate it and do it separately as an independent operator."
Most often used when you use the inline string. Say:
$x = Get-ChildItem C:\; $x | ForEach-Object { Write-Output "The file is $($_.FullName)"; }
Compare this to:
$x = Get-ChildItem C:\; $x | ForEach-Object { Write-Output "The file is $_.FullName"; }
You can also do things like $($x + $y).ToString() or $(Get-Date).AddDays(10) .
Here, without a subexpression, you get $a:\calendar . Well, the problem is that the colon after the variable is an operator. In particular, the domain operator . So that PowerShell does not think that you are trying to find a variable in the namespace a , the author puts the variable in a subexpression.
As far as I could tell using PS over the past few years, dollar-free parentheses are also essentially sub-expressions. They will not be evaluated as a subexpression if inside the string, but otherwise they will usually be. This is a kind of frustrating quirk that there is no clear difference.
source share