{"id":68713,"date":"2005-10-19T16:49:00","date_gmt":"2005-10-19T16:49:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/10\/19\/hey-scripting-guy-how-can-i-configure-the-hide-from-exchange-address-lists-property-for-all-the-contacts-in-a-domain\/"},"modified":"2005-10-19T16:49:00","modified_gmt":"2005-10-19T16:49:00","slug":"hey-scripting-guy-how-can-i-configure-the-hide-from-exchange-address-lists-property-for-all-the-contacts-in-a-domain","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-configure-the-hide-from-exchange-address-lists-property-for-all-the-contacts-in-a-domain\/","title":{"rendered":"Hey, Scripting Guy! How Can I Configure the Hide From Exchange Address Lists Property for All the Contacts in a Domain?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>Hey, Scripting Guy! How can I configure the <B>Hide From Exchange Address Lists<\/B> property for all the contacts in a domain?<BR><BR>&#8212; MM<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, MM. You know what the say: you can run, but you can\u2019t hide (especially from Microsoft Exchange!). But what they forgot to add is that you <I>can<\/I> hide, as long as you\u2019ve set the value of the MSExchHideFromAddressLists attribute to True.<\/P>\n<P>But you already knew that, didn\u2019t you? What you <I>really<\/I> wanted to know was how you can configure this attribute for each and every contact in your domain, wasn\u2019t it? Well, that\u2019s easy; you just use a script like this one:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const ADS_SCOPE_SUBTREE = 2<\/p>\n<p>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)\nSet objCommand =   CreateObject(&#8220;ADODB.Command&#8221;)\nobjConnection.Provider = &#8220;ADsDSOObject&#8221;\nobjConnection.Open &#8220;Active Directory Provider&#8221;\nSet objCommand.ActiveConnection = objConnection<\/p>\n<p>objCommand.Properties(&#8220;Page Size&#8221;) = 1000\nobjCommand.Properties(&#8220;Searchscope&#8221;) = ADS_SCOPE_SUBTREE <\/p>\n<p>objCommand.CommandText = _\n    &#8220;SELECT ADsPath FROM &#8216;LDAP:\/\/dc=fabrikam,dc=com&#8217; WHERE objectClass=&#8217;contact'&#8221;  \nSet objRecordSet = objCommand.Execute<\/p>\n<p>objRecordSet.MoveFirst\nDo Until objRecordSet.EOF\n    strContactPath = objRecordSet.Fields(&#8220;ADsPath&#8221;).Value\n    Set objContact = GetObject(strContactPath)\n    objContact.MSExchHideFromAddressLists = TRUE\n    objContact.SetInfo\n    objRecordSet.MoveNext\nLoop\n<\/PRE>\n<P>If you look closely at the code (and we always hope that you <I>do<\/I> look closely at the code and don\u2019t just take our word for it), you\u2019ll see that this is an example of an Active Directory search script. As usual, we won\u2019t take the time to explain each and every line of code here; if you need some background information on searching Active Directory, we recommend you take a look at our two-part series, <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/tales\/sg0405.mspx\"><B>Dude, Where\u2019s My Printer?<\/B><\/A> For now we\u2019ll merely note that this is the query we use to retrieve a list of all the contacts in the domain:<\/P><PRE class=\"codeSample\">objCommand.CommandText = _\n    &#8220;SELECT ADsPath FROM &#8216;LDAP:\/\/dc=fabrikam,dc=com&#8217; WHERE objectClass=&#8217;contact'&#8221;\n<\/PRE>\n<P>Yes, very simple: all we do is request all the items in Active Directory where the <B>objectClass<\/B> attribute is equal to <I>contact<\/I>.<\/P>\n<P>Of course, then the question becomes this: once we have a collection of all the contacts what the heck do we do with it? <\/P>\n<P>Well, as you probably know, Active Directory search scripts are read-only: although we can use queries to retrieve data from Active Directory we can\u2019t write any sort of Update query that can go out and <I>modify<\/I> that data. Instead, what we need to do is individually bind to each contact and change the value of the MSExchHideFromAddressLists attribute.<\/P>\n<P>Don\u2019t worry; it\u2019s not as hard as it sounds. (We\u2019ve already done the hard part, retrieving a list of all the contacts in the domain.) To begin with, we set up a Do Until loop that will walk us through our recordset, contact-by-contact (the <B>EOF<\/B>, of course, is short for <I>end of file<\/I>, meaning this loop will keep running until we run out of records):<\/P><PRE class=\"codeSample\">Do Until objRecordSet.EOF\n<\/PRE>\n<P>For each contact we grab the value of the <B>ADsPath<\/B> attribute and then use the <B>GetObject<\/B> method to bind to the contact account in Active Directory. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">strContactPath = objRecordSet.Fields(&#8220;ADsPath&#8221;).Value\nSet objContact = GetObject(strContactPath)\n<\/PRE>\n<P>The ADsPath attribute, of course, looks something like this:<\/P><PRE class=\"codeSample\">LDAP:\/\/cn=Ken Myer, ou=Finance, dc=fabrikam, dc=com\n<\/PRE>\n<P>That\u2019s why you don\u2019t see us reference the LDAP provider when we call GetObject: we don\u2019t need to reference the provider because it\u2019s included in the ADsPath.<\/P>\n<P>After binding to the contact account we set the MSExchHideFromAddressLists attribute to True, then call the <B>SetInfo<\/B> method to write the updated information to Active Directory:<\/P><PRE class=\"codeSample\">objContact.MSExchHideFromAddressLists = TRUE\nobjContact.SetInfo\n<\/PRE>\n<P>Don\u2019t leave out the SetInfo method: if you do, the changes you make will not be saved to the contact account in Active Directory. And don\u2019t leave out this line of code, either:<\/P><PRE class=\"codeSample\">objRecordSet.MoveNext\n<\/PRE>\n<P>This is the cue for the script to move from one record in the collection to the next. If you leave this out, your script will continue working with the first record in the collection from now until the end of time. (Which, in some ways, would be kind of cool, but not exactly what you had in mind.)<\/P>\n<P>And that\u2019s it: your contacts will now be hidden from the Exchange address lists. Now if only <I>we<\/I> could hide from email that easily \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I configure the Hide From Exchange Address Lists property for all the contacts in a domain?&#8212; MM Hey, MM. You know what the say: you can run, but you can\u2019t hide (especially from Microsoft Exchange!). But what they forgot to add is that you can hide, as long as you\u2019ve [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,19,9,3,4,5],"class_list":["post-68713","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-activex-data-objects-ado","tag-adsi","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I configure the Hide From Exchange Address Lists property for all the contacts in a domain?&#8212; MM Hey, MM. You know what the say: you can run, but you can\u2019t hide (especially from Microsoft Exchange!). But what they forgot to add is that you can hide, as long as you\u2019ve [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68713","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=68713"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68713\/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=68713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}