{"id":70173,"date":"2005-03-23T17:04:00","date_gmt":"2005-03-23T17:04:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/23\/how-can-i-set-an-active-directory-attribute-value-to-null\/"},"modified":"2005-03-23T17:04:00","modified_gmt":"2005-03-23T17:04:00","slug":"how-can-i-set-an-active-directory-attribute-value-to-null","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-set-an-active-directory-attribute-value-to-null\/","title":{"rendered":"How Can I Set an Active Directory Attribute Value to NULL?"},"content":{"rendered":"<p><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\"> \n<P>Hey, Scripting Guy! How can I set an Active Directory property to NULL? I tried setting the value to an empty string (\u201c\u201d) but it didn\u2019t work.<BR><BR>&#8212; JJ<\/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, JJ. You know, it\u2019s not like the Scripting Guys to get philosophical; one of the Scripting Guys, for example, bases his entire life on this credo: never answer the phone early on Saturday morning. (His reasoning is fairly straightforward: if it\u2019s important, they\u2019ll call back. And if it\u2019s not important, then why the heck are they calling early on Saturday morning?!?!) That\u2019s about as deep as the Scripting Guys ever get.<\/P>\n<P>For better or worse, though, we have to get a little philosophical &#8211; and maybe even a little metaphysical &#8211; when trying to answer this question. The problem you are running into is the fact that while you might think an empty string (\u201c\u201d) is nothing, scripting languages tend to see an empty string as something. For example, suppose you set User A\u2019s telephone number to an empty string. You then run a script that retrieves a list of all the users who have telephone numbers. Guess who shows up in that list? That\u2019s right: good old User A. That\u2019s because, in the crazy world of scripting, User A actually <I>has<\/I> a telephone number; it\u2019s just that his telephone number happens to consist of an empty string. (Yes, sort of a sound-of-one-hand-clapping thing.)<\/P>\n<P>If you want to get rid of an attribute value altogether you have to set the value of that attribute to NULL. And in Active Directory the best way to do that is to use the <B>PutEx<\/B> method and clear the value. Let\u2019s show you a script that truly <I>does<\/I> set a user\u2019s telephone number to nothing (NULL) and then we\u2019ll explain how it works:<\/P><PRE class=\"codeSample\">Const ADS_PROPERTY_CLEAR = 1 <\/p>\n<p>Set objUser = GetObject _\n   (&#8220;LDAP:\/\/cn=ken myer, ou=finance, dc=fabrikam, dc=com&#8221;) <\/p>\n<p>objUser.PutEx ADS_PROPERTY_CLEAR, &#8220;telephoneNumber&#8221;, 0\nobjUser.SetInfo\n<\/PRE>\n<P>We begin by defining a constant named ADS_PROPERTY_CLEAR and setting the value to 1; we\u2019ll use this later in the script to tell the PutEx method the kind of operation we want to carry out. (PutEx has additional uses beyond clearing attribute values; for more information, check out the <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_ads_overview.mspx\"><B>ADSI Scripting Primer<\/B><\/A> in the <I>Microsoft Windows 2000 Scripting Guide<\/I>.) We then bind to the desired user account, in this case the Ken Myer account found in the Finance OU of fabrikam.com<\/P>\n<P>Next we use these two lines of code to completely erase Ken Myer\u2019s telephone number:<\/P><PRE class=\"codeSample\">objUser.PutEx ADS_PROPERTY_CLEAR, &#8220;telephoneNumber&#8221;, 0\nobjUser.SetInfo\n<\/PRE>\n<P>As you can see, we call the PutEx method and pass it three parameters: 1) the constant <B>ADS_PROPERTY_CLEAR<\/B> which, again, indicates the operation we want to perform; 2) <B>telephoneNumber<\/B>, the attribute we want to clear; and 3) <B>0<\/B>, a parameter required any time you clear an attribute. We then call the <B>SetInfo<\/B> method to write the changes back to Active Directory. If you re-run the script that retrieves a list of users who have phone numbers, Ken Myer will no longer appear in the list. That\u2019s because he no longer has a phone number, not even one consisting of an empty string.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I set an Active Directory property to NULL? I tried setting the value to an empty string (\u201c\u201d) but it didn\u2019t work.&#8212; JJ Hey, JJ. You know, it\u2019s not like the Scripting Guys to get philosophical; one of the Scripting Guys, for example, bases his entire life on this credo: [&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,3,20,5],"class_list":["post-70173","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-user-accounts","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I set an Active Directory property to NULL? I tried setting the value to an empty string (\u201c\u201d) but it didn\u2019t work.&#8212; JJ Hey, JJ. You know, it\u2019s not like the Scripting Guys to get philosophical; one of the Scripting Guys, for example, bases his entire life on this credo: [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70173","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=70173"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70173\/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=70173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}