Generally, when using Environment Variables I stick to ${env:foo}, but have used $($env:foo) in the past. These both seem to evaluate the same whenever I’ve used them. This seems to be the case for when I do $($foo) or ${foo} as well.
Is there a functional difference between using ${env:foo} or ${foo} and $($env:foo) or $($foo)?
yes, $() is called sub expression, this is mainly used to place an expression between a string.
eg:-
$Process = Get-Process | Select-Object -First 1
Write-Output -InputObject "Process ID is $Process.Id" # Without subexpression
Write-Output -InputObject "Process ID is $($Process.Id)" # With subexpression
${} is used when we use space or any other characters in variable which cannot be normally used, this is also used as provider paths.
${This is also a variable} = 'Variable Value'
${This is also a variable}
Function TestFunction {
Write-Output -InputObject "Empty Function"
}
${Function:TestFunction} # This returns the function content as scriptblock for function provider
'test file' > c:\temp\test.txt
${c:\temp\test.txt} # gets the content of the file for filesystem provider
# for available providers
Get-PSProvider
Gives the list of ProcessNames, ${foo.ProcessName} does nothing because 'foo.ProcessName' as a variable doesn’t exist.
I guess the heart of my question is, should I be always using $($foo) instead of ${foo} when using simple variable names? Is there anything wrong with using ${foo}?
Gives the list of ProcessNames, ${foo.ProcessName} does nothing because foo.ProcessName as a variable doesn’t exist.
I guess the heart of my question is, should I be always using $($foo) instead of ${foo} when using simple variable names? Is there anything wrong with using ${foo}?
$() sub expressions are never used until there is a requirement to use access members of an object inside quotes. And ${} also either for provider paths or variables with specific names which are normally not supported in variables names. Otherwise its only $ to be used. But there is no hard rule as $() and ${} cannot be used for simple variable names.
Thanks! I don’t go out of my way to enclose variables in any form of brackets/braces, but for simple variables I think ${foo} looks better than $($foo), and if there’s no rule, or functional difference for simple variables, I’ll keep at it.