Wednesday, October 9, 2013

hyphen problems in code

Today's painful IT lesson was on the difference between a hyphen, an en-dash and an em-dash.

While testing dynamic distribution lists I got stuck on the following two recipient filters, which look identical, and yet the first will run fine and the second one will error out.

-RecipientFilter {(RecipientType -eq "UserMailbox") -and (ExtensionCustomAttribute1 -like "Exchange")} -WhatIf
-RecipientFilter {(RecipientType -eq "UserMailbox") -and (ExtensionCustomAttribute1 –like "Exchange")} -WhatIf

Taking a closer look at the code, the actual problem is right here.

(ExtensionCustomAttribute1 -like "Exchange")
(ExtensionCustomAttribute1 –like "Exchange")

In most text editors the hyphen before the "like" appear to the human eye to be the same.

As it turns out, the first is a hyphen while the second one is an en-dash.  Some text editors such as Word will display them differently and even autocorrect during certain situations between one and the other.  They are however two completely different characters.

Pasting them into a text to html converter yields the following:


Pasting them into a text to ASCII converter yields the following:

226 128 147

Regardless, trying to figure out where in the code things went wrong and doing a visual comparsion in a text editor will drive you mad for sure.  I suppose the lesson is to be careful of what we use when we copy and paste.

Further reading on the subject can be found in the below article: