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

Welcome Forums General PowerShell Q&A Functional difference between ${variable} and $($variable)

  • This topic has 7 replies, 3 voices, and was last updated 1 month ago by
    js
    Participant
    .
Viewing 6 reply threads
  • Author
    Posts
    • #265928
      Participant
      Topics: 1
      Replies: 3
      Points: 25
      Rank: Member

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

    • #265979
      Senior Moderator
      Topics: 9
      Replies: 1420
      Points: 5,280
      Helping Hand
      Rank: Community MVP

      yes, $() is called sub expression, this is mainly used to place an expression between a string.
      eg:-

      ${} 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 reply was modified 1 month ago by grokkit.
    • #266090
      Participant
      Topics: 1
      Replies: 3
      Points: 25
      Rank: Member

      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:

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

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

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

    • #266111
      Participant
      Topics: 1
      Replies: 3
      Points: 25
      Rank: Member

      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:

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

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

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

    • #266159
      Senior Moderator
      Topics: 9
      Replies: 1420
      Points: 5,280
      Helping Hand
      Rank: Community MVP

      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.

    • #266162
      Participant
      Topics: 1
      Replies: 3
      Points: 25
      Rank: Member

      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!

    • #266519
      js
      Participant
      Topics: 32
      Replies: 850
      Points: 2,751
      Helping Hand
      Rank: Community Hero

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

      • This reply was modified 1 month ago by js.
Viewing 6 reply threads
  • You must be logged in to reply to this topic.