Every pithy witticism begins with quotation marks
“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:![]()
Read the article and download the wallchart here.
Related Articles
The Ternary Cometh
Developers are likely to be familiar with ternary conditional operators as they’re legal in many languages (Ruby, …
Read moreLearn 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 …
Read moreSecure 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 …
Read more