"Smart" quotes are a feature in Microsoft Word that supposedly help editors notice quotes better? I'm not sure, nor do I care. In my world, a quote is a quote, and a PowerShell script has no idea what a "smart" quote is.

Have you ever stopped to wonder whether a particular character on your keyboard needed to be "smart?" I haven't, but someone on the Office productivity team at Microsoft did, and it's been leaving developers and scripters annoyed for a long time now.

You'll sometimes come across a problem when running PowerShell code when you nonchalantly copy and paste code from a Word document directly into a script. When you do this, you're copying "smart" quotes rather than standard quotes because most text editors keep the formatting.

To demonstrate, let's say I have some code saved in a Word document that looks like this:

PowerShell code with smart quotes in Word

PowerShell code with smart quotes in Word

You can barely see the quotes in that Word document don't look "normal." They're a bit angled. That indicates smart quotes. I'll now copy that text into Notepad and save the file as C:\test.ps1 and execute it.

It works! What's the problem, you may ask? It works, but only because PowerShell converts the smart quotes to "normal” quotes. You could just be OK with this, but there are times when these "smart" quotes become a problem. Running on different PowerShell versions, code consumed by other applications, and other factors are all culprits of the problem. The system does not interpret these smart quotes the same 100% of the time.

It's always best to dumb down these smart quotes by replacing them. To replace text in PowerShell, I typically use -replace like this: $string -replace 'replacethis','withthis'. This means all we have to do is find these smart quotes and then just replace them with good ol' fashioned quotes, right? No. Unfortunately, there's no smart quote character on your keyboard. Instead, we have to define those characters via a character map. Smart double quotes use both \u201D and \u201C while \u2019 and \u2018 represent single smart quotes. We need to look for and replace these characters.

To make this replacement easier, we can create a couple regular expressions and assign them to variables.

Once we've defined those, we can now search any text file for these characters and replace them with "normal" single and double quotes using Get-Content and the -replace operator.

We can then look at our original text file (a script in this case) and see our script has replaced all instances of double and single smart quotes as we expect!

Always pay attention to smart quotes and formatting when copying and pasting text from Word and other editors. If possible, always treat text as plain text to prevent problems like this from popping up in the first place!

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

4+
avatar
Share
1 Comment
  1. Bucky 6 months ago

    VS Code extension Unicode Substitutions does a great job of fixing this issue.

    https://marketplace.visualstudio.com/items?itemName=GlenBuktenica.unicode-substitutions

    0

Leave a reply

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

*

© 4sysops 2006 - 2020

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account