The Power of “$()” in PowerShell

Sometimes, and sometimes all the time, I like to cram as much as I can into a single line of PowerShell code. It’s like a challenge to me, making a command as long as absolutely possible. I haven’t quite broken the 1000 character mark, but I have made it to the 600+ character milestone. I’m sure professional programmers are cringing and mumbling something about spaghetti right now, but I think it’s fun. 

The best tool, besides pipes, for doing this in PowerShell is $().  What is $() you may ask? Officially…I have no idea. Unofficially it means “return the value of whatever’s inside.”  It’s like a spontaneous function or script block.  It is a very handy tool that all PowerShell’ers should know. 

Let me give you an example.  Say you want to write the output of, oh, I don’t know, the product key of your TechNet licenses using my handy keys.xml to keys.txt converter script (http://www.orcsweb.com/blog/james/using-powershell.../), but you want to add a conditional statement if there is a claim date attached to the key.  You have some options.

blog comments powered by Disqus
Tagged as: