What does $() mean in PowerShell?

Unlike my last post, this one is a naked attempt to get clicks.

See, today Google emailed me with a suggestion to try it’s “experimental” content ideas, saying that I could “get inspiration for new content from Google” for a limited time only.

I’m unclear on whether this is Google saying, “Blog about this and we’ll increase your search rankings” or if they are saying, “Here’s a search term we want to increase the auction rate for.” Maybe, it’s both. Take a look at a screenshot of the topics they suggested for this blog. Note that they got the main categories right — but the suggested topics seem oddly mechanical.

Google Search suggested content topics (click to enlarge)

In any case, who am I to argue with Google? So, here’s an easy explanation of what $() means in PowerShell. (I thought about blogging on the potentially charged question, “Do I need AWS?” But you can see why that would’ve gone nowhere.)

$(), or “dollar parentheses,” is a PowerShell sub-expression, documented in the usual terse PowerShell way in about-Operators. In plain English, a sub-expression tells PowerShell to evaluate the expression contained in the parentheses.

It’s a quick and useful way to make strings out of parts of objects. One often needs to do this when an object has been returned and you want to write it out via Export-Csv.

What trips most people up is when they write an object to the console, PowerShell converts it for you. But if you are going to use the object in any other way, you are responsible for transforming it — and making the data you want into a string using a sub-expression is a handy way to do so. I blogged about this years ago in a discussion of how to nicely format Azure PowerShell objects.

Well, there you go, Google. I’ve done what you told me to. Now what? 🙂


Posted

in

by

Tags:

Comments

2 responses to “What does $() mean in PowerShell?”

  1. Mr IT Avatar
    Mr IT

    I had struggles so bad with the OPATH syntax last night on a critical script. As a last ditch effort to stop myself from nearly throwing my laptop across the office out of absolute frustration, my peer comes over, looks at my script and says, you’re missing this $ Rage, laughter and embarrassment were all mixed into one. I did not know
    that is was needed to tell the filter that it was a “sub-expression” and needed to evaluate the inner content. Thanks!

    1. Alex Neihaus Avatar
      Alex Neihaus

      I really love pwsh and use it all day everyday. But it can be obscure. You really need that 10K hours-of-use investment.

      I’m pleased to have been of assistance.

Leave a Reply

Your email address will not be published. Required fields are marked *