“Microsoft Office 365: Exchange Online Implementation and Migration” tells that we need to check following users’ attributes in regards to occurrence special characters:
sAMAccountName: Invalid character examples are !, #, $, %, ^, &, {, }, \, /, `, ~, “, [, ], :, @, <, >, +, =, ;, ?, and *
displayName: Invalid character examples are ?, @, and +
mail: Invalid character examples are [, !, #, $, %, &, *, +, \, /, =, ?, ^, `, {, }, and ]
mailNickname: Invalid character examples are “, \, /, [, ], :, >, <, ;, and spaces
proxyAddresses: Invalid character examples are [, !, #, $, %, &, *, +, \, /, =, ?, ^, `, {, }, and ]
userPrincipalName: Invalid character examples are }, {, #, ‘, *, +, ), (, >, <, \, /, =, ?, and `
Well, you agree that nothing easier. So I wrote a simple script that was checking for it, but to my surprise, I noticed that in 3 of the attributes I was getting “$”, “*” and “?” characters. But attributes definitely didn’t have that value, so I replaced “-match” with “-like” in the script.
Well, it was better but still not brill. I was still getting $ and *. But the ? char was not being reported. I started to think what the hell is wrong felling a bit like:

I was using “-match” and “-like” all the time in scripts, I was shocked it was messing around with me. So I started to check if there are any other way to compare if char exist in a string. And there it was! Totally forgot about “contains” operator/method!
Changed all my “matches” and “likes” to “-contains” and taaaddaaam 🙂 All worked like a charm!
Answer to question why it might have not worked can be found for example under here:
http://www.tjrobinson.net/?p=109
Tom is telling there that:
-contains is designed to work on arrays, not strings (referred to as a containment operator)
-match looks for a match inside a string and supports regexes
–like looks for a match inside a string and supports wildcards
And here is the thing, my solution was about to check if string, ex. “mail” contains: one of [, !, #, $, %, &, *, +, \, /, =, ?, ^, `, {, }, ] . Syntax was:
$Characters_To_Check = @(“[“, “!”, “#”,”$”, “%”, “&”, “*”, “+”, “\”, “/”, “=”, “?”, “^”, “`”, “{“, “}”, “]”)
$String_To_Check = “$User.mail”
Foreach ($Character in $Characters_To_Check ) {
if ($String_To_Check -match $Character) {Do things}
}
because “match” and “like” operators support regex and wildcards, they were treating these special characters as these. Only contains was working properly in that particular case.
Like this:
Like Loading...