{"id":80296,"date":"2016-10-14T11:59:30","date_gmt":"2016-10-14T18:59:30","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=80296"},"modified":"2019-02-18T09:10:25","modified_gmt":"2019-02-18T16:10:25","slug":"powertip-how-to-detect-a-valid-active-directory-user-name-using-regular-expressions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powertip-how-to-detect-a-valid-active-directory-user-name-using-regular-expressions\/","title":{"rendered":"PowerTip: How to detect a valid Active Directory user name using regular expressions"},"content":{"rendered":"<p><strong>Summary<\/strong>: Cloud and Datacenter Management MVP, Thomas Rayner, shows how use regex to detect if a string is a valid Active Directory user name.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/> The invalid characters for an Active Directory user name are <code>\/\\[:;|=,+*?&lt;&gt;]\\<\/code> and <code>\u201c<\/code>. How do I tell if a string is a valid Active Directory user name?<\/p>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/> You can treat this array of characters as a pattern and see if they exist within the string<\/p>\n<p style=\"padding-left: 90px\"><code>$questionablestring -match \"[$([Regex]::Escape('\/\\[:;|=,+*?&lt;&gt;') + '\\]' + '\\\"')]\"<\/code><\/p>\n<p style=\"padding-left: 60px\">You\u2019ll have to manually escape <code>]<\/code> and <code>\u201c<\/code> because <code>[regex]::escape()<\/code> is a bit quirky.<\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7610.Dr.ScriptoForTips.jpg\" alt=\"The Doctor\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Cloud and Datacenter Management MVP, Thomas Rayner, shows how use regex to detect if a string is a valid Active Directory user name. The invalid characters for an Active Directory user name are \/\\[:;|=,+*?&lt;&gt;]\\ and \u201c. How do I tell if a string is a valid Active Directory user name? &nbsp; You can treat [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568,639,687,641],"tags":[56,356,652,45],"class_list":["post-80296","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-powertip","category-regular-expressions","category-windows-powershell","tag-guest-blogger","tag-powertip","tag-thomas-rayner","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Cloud and Datacenter Management MVP, Thomas Rayner, shows how use regex to detect if a string is a valid Active Directory user name. The invalid characters for an Active Directory user name are \/\\[:;|=,+*?&lt;&gt;]\\ and \u201c. How do I tell if a string is a valid Active Directory user name? &nbsp; You can treat [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=80296"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/80296\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=80296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=80296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=80296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}