- Create a certificate-signed RDP shortcut via Group Policy - Fri, Aug 9 2019
- Monitor web server uptime with a PowerShell script - Tue, Aug 6 2019
- How to build a PowerShell inventory script for Windows Servers - Fri, Aug 2 2019
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:
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.
PS> C:\test.ps1 foo
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.
$smartSingleQuotes = '[\u2019\u2018]' $smartDoubleQuotes = '[\u201C\u201D]'
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.
PowerShell $filePath = 'C:\test.ps1' $content = Get-Content -Path $filePath -Raw $content = $content -replace $smartSingleQuotes,"'" $content -replace $smartDoubleQuotes,'"' | Set-Content -Path $filePath
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!
Subscribe to 4sysops newsletter!
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!