PowerShell for Admins PowerShell for Developers Tips and Tricks Tutorials

Every pithy witticism begins with quotation marks

msorens
2 min read
Share:

“To be or not to be”. Without getting into a debate over whether Shakespeare was musing about being a logician, suffice to say that in writing prose, the rules of when and how to use quotation marks are relatively clear. In PowerShell, not so much. Sure, there is an about_Quoting_Rules documentation page, and that is a good place to start, but that barely covers half the topic. It assumes you need quotes and then helps you appreciate some of the factors to consider when choosing single quotes or double quotes.
But do you need quotes? Remember PowerShell is a shell/command language so “obviously” you can do things like this:

PS> Delete-Item C:\tmp\foobar.txt PS> Get-ChildItem *.log PS> Get-Process svchost, conhost, powershell It would certainly be cumbersome if you needed to quote each of those arguments, so PowerShell was designed well, in that respect.
But what if you ran the same commands just slightly differently?

PS> "C:\tmp\foobar.txt" | Delete-Item PS> "*.log" | Get-ChildItem Here you must use quotation marks or you will suffer the wrath of a terminating error from the PowerShell host most certainly!
Those are just a couple of the many examples I consider in When to Quote in PowerShell. Accompanying the full article, I also included a wallchart that condenses all the article’s salient points into a single-page reference. Here’s a fragment of the wallchart:
Guide to PowerShell Quoting wall chart
Read the article and download the wallchart here.

Related Articles

Sep 12, 2019

The Ternary Cometh

Developers are likely to be familiar with ternary conditional operators as they’re legal in many languages (Ruby, Python, C++, etc). They’re also often used in coding interviews to test an applicant as they can be a familiar source of code errors. While some developers couldn’t care less about ternary operators, there’s been a cult following waiting for them to show up in Powershell. That day is almost upon us. Any Powershell developer can easily be forgiven for scratching their heads and wondering what a ternary is.

Apr 18, 2019

Learn To Use Verbose Output Streams In Your Pester Tests

I’m going to file this under “Either I’m a genius, or there’s a much better way and everyone knows it except for me.” I recently began adding a suite of Pester tests to one of my projects and I found myself needing to mock some unit tests against a particular function that would modify a variable based on the parameter specified. Since all the functions I write nowadays are considered advanced functions (and yours should be too, they’re free!

Mar 28, 2019

Secure Your Powershell Session with JEA and Constrained Endpoints

Index What is a Constrained Endpoint and Why Would I Need One? Setup and Configuration Using our Endpoint What is a constrained endpoint and why would I need one? Powershell constrained endpoints are a means of interacting with powershell in a manner consistent with the principal of least privilege. In Powershell terms, this is referred to as Just-Enough-Administration, or JEA. JEA is very well documented, so this won’t simply be repeating everything those references detail.