# Functional difference between ${variable} and $($variable)

**URL:** <https://forums.powershell.org/t/functional-difference-between-variable-and-variable/15144>\
**Category:** PowerShell Help\
**Created:** [October 25, 2020, 8:07pm UTC](https://forums.powershell.org/t/functional-difference-between-variable-and-variable/15144 "2020-10-25T20:07:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![robbiecrash](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/robbiecrash/32/48_2.png) [@robbiecrash](https://forums.powershell.org/u/robbiecrash)\
**Post date:** [October 25, 2020, 8:07pm UTC](https://forums.powershell.org/t/functional-difference-between-variable-and-variable/15144/1 "2020-10-25T20:07:27Z")

</div>

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)_?

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [October 26, 2020, 12:38am UTC](https://forums.powershell.org/t/functional-difference-between-variable-and-variable/15144/2 "2020-10-26T00:38:13Z")

</div>

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
```

```
```

---

<div class="post-metadata">

**Author:** ![robbiecrash](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/robbiecrash/32/48_2.png) [@robbiecrash](https://forums.powershell.org/u/robbiecrash)\
**Post date:** [October 26, 2020, 12:37pm UTC](https://forums.powershell.org/t/functional-difference-between-variable-and-variable/15144/3 "2020-10-26T12:37:08Z")

</div>

Thanks for the concise and informative answer! `${function:}` is new to me and super helpful!

For simple variables, is there any difference in how they’re parsed? IE:

```
$foo = "bar"
Write-Output $foo
Write-Output $($foo)
Write-Output ${foo}
```

All seem to yield identical results. Is the only difference here style?

When calling properties of the object, clearly there’s a difference:

```
$foo = Get-Process "SomeFilter"
Write-Output $Foo.ProcessName
Write-Output $($foo.ProcessName)
Write-Output ${foo}.ProcessName
```

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}`?

---

<div class="post-metadata">

**Author:** ![robbiecrash](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/robbiecrash/32/48_2.png) [@robbiecrash](https://forums.powershell.org/u/robbiecrash)\
**Post date:** [October 26, 2020, 2:30pm UTC](https://forums.powershell.org/t/functional-difference-between-variable-and-variable/15144/4 "2020-10-26T14:30:45Z")

</div>

I’m not sure what I did to my last post, but it seems to be in my history, but not on the thread, so here it is again:

Thanks for the concise and informative answer! `${function:}` is new to me and super helpful!

For simple variables, is there any difference in how they’re parsed? IE:

```
$foo = "bar"
Write-Output $foo
Write-Output $($foo)
Write-Output ${foo}
```

All seem to yield identical results. Is the only difference here style?

When calling properties of the object, clearly there’s a difference:

```
$foo = Get-Process "SomeFilter"
Write-Output $Foo.ProcessName
Write-Output $($foo.ProcessName)
Write-Output ${foo}.ProcessName
```

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}`?

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [October 26, 2020, 7:45pm UTC](https://forums.powershell.org/t/functional-difference-between-variable-and-variable/15144/5 "2020-10-26T19:45:28Z")

</div>

> [@](#):
>
> I guess the heart of my question is, should I be always using $($foo) instead of ${foo} when using&nbsp;simple variable names? Is there anything wrong with using&nbsp; ${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.

---

<div class="post-metadata">

**Author:** ![robbiecrash](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/robbiecrash/32/48_2.png) [@robbiecrash](https://forums.powershell.org/u/robbiecrash)\
**Post date:** [October 26, 2020, 8:25pm UTC](https://forums.powershell.org/t/functional-difference-between-variable-and-variable/15144/6 "2020-10-26T20:25:37Z")

</div>

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.

Thanks for your detailed explanations!

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [October 28, 2020, 7:20am UTC](https://forums.powershell.org/t/functional-difference-between-variable-and-variable/15144/7 "2020-10-28T07:20:10Z")

</div>

$( ) has other uses too. You can combine multiple statements into one.

```
echo hi; echo there | measure | % count
hi
1

$(echo hi; echo there) | measure | % count
2
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/functional-difference-between-variable-and-variable/15144/8 "2024-05-16T20:28:41Z")

</div>


