{"id":66143,"date":"2006-10-31T14:14:00","date_gmt":"2006-10-31T14:14:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/10\/31\/how-can-i-configure-an-active-directory-account-so-the-password-never-expires\/"},"modified":"2006-10-31T14:14:00","modified_gmt":"2006-10-31T14:14:00","slug":"how-can-i-configure-an-active-directory-account-so-the-password-never-expires","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-configure-an-active-directory-account-so-the-password-never-expires\/","title":{"rendered":"How Can I Configure an Active Directory Account So the Password Never Expires?"},"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 configure an Active Directory user account so that the account password never expires?<BR><BR>&#8212; BB<\/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, BB. Before we start we have a question for <I>you<\/I>: wouldn\u2019t you agree that <I>Catch-22<\/I> is one of the four greatest novels ever written? We ask that because, in his English class, the Scripting Son was recently asked to select, and read, a book from a list provided by the teacher. \u201cHey, Scripting Dad,\u201d he said. \u201cHave you read any of these books? Are any of them any good?\u201d<\/P>\n<P>\u201cGood heavens, Scripting Son!\u201d replied the Scripting Dad. \u201c<I>Catch-22<\/I> is on the list! <I>Catch-22<\/I> is one of the four greatest novels ever written. <I>Everyone<\/I> knows that.\u201d<\/P>\n<P>\u201cOK, I guess I\u2019ll read that one,\u201d replied the Scripting Son.<\/P>\n<P>Of course, ever since then the Scripting Dad has had to listen to an unending litany of complaints:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\u201cThis book is weird.\u201d<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\u201cThis book doesn\u2019t make any sense.\u201d<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\u201cI thought you said this was a <I>good<\/I> book.\u201d<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\u201cThis is all your fault.\u201d<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<TABLE id=\"E3D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Actually that last one has nothing to do with <I>Catch-22<\/I>. That\u2019s just a standard complaint the Scripting Dad hears every day.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In order to fight fire with fire, the Scripting Dad is looking for people who agree with him that <I>Catch-22<\/I> is one of the four greatest novels ever written. Can he count on your support, BB? OK, what if he sweetens the deal by giving you a script that can configure an Active Directory user account so that the account password never expires:<\/P><PRE class=\"codeSample\">Const ADS_UF_DONT_EXPIRE_PASSWD = &amp;h10000<\/p>\n<p>Set objUser = GetObject(&#8220;LDAP:\/\/cn=Ken Myer, ou=Finance, dc=fabrikam, dc=com&#8221;)<\/p>\n<p>intUserAccountControl = objUser.Get(&#8220;userAccountControl&#8221;)<\/p>\n<p>If Not objUser.userAccountControl AND ADS_UF_DONT_EXPIRE_PASSWD Then\n    objUser.Put &#8220;userAccountControl&#8221;, _\n        objUser.userAccountControl XOR ADS_UF_DONT_EXPIRE_PASSWD\n    objUser.SetInfo\nEnd If\n<\/PRE>\n<P>What do you mean, \u201cThat\u2019s still not good enough\u201d? Wow; you\u2019re a tough negotiator, BB. OK, how about this: how about we explain how this script works? Deal? Deal.<\/P>\n<P>As you can see, the script starts out by defining a constant named ADS_UF_DONT_EXPIRE_PASSWD and assigning this constant the hexadecimal value &amp;h10000. We\u2019ll need this constant when we reconfigure the account so that its password never expires.<\/P>\n<P>After defining the constant we connect to the Ken Myer user account in Active Directory. Because Ken has a CN equal to <I>Ken Myer<\/I> and because his account is in the Finance OU of Fabrikam.com the code required to connect to that account looks like this:<\/P><PRE class=\"codeSample\">Set objUser = GetObject(&#8220;LDAP:\/\/cn=Ken Myer, ou=Finance, dc=fabrikam, dc=com&#8221;)\n<\/PRE>\n<P>Now it starts to get a little weird (but just a little). As it turns out, the Active Directory property that determines whether or not a password expires is contained within the <B>userAccountControl<\/B> property. The userAccountControl property is a \u201cbitmask\u201d property: that means it\u2019s a single property that contains multiple values. Is that important? You bet it is: because userAccountControl contains multiple values we can\u2019t simply set it to, say, True or False. Instead, we need to use \u201cbitwise logic\u201d to toggle the particular property that controls password expiration. That\u2019s just a heads-up to explain why some of the code you\u2019re about to see will look a little unusual, to say the least.<\/P>\n<TABLE id=\"E4E\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. We won\u2019t to explain bitwise logic in any detail today; for more information, see <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_usr_neov.mspx\"><B>this section<\/B><\/A> of the <I>Microsoft Windows 2000 Scripting Guide<\/I>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Our next step is to determine whether or not the account already <I>has<\/I> a password that doesn\u2019t expire; after all, if Ken already has a non-expiring password then we don\u2019t need to do anything, do we? Using our bitwise logic we employ this line of code to determine if the \u201cpassword doesn\u2019t expire\u201d property (represented by the constant ADS_UF_DONT_EXPIRE_PASSWD) is <I>not<\/I> enabled (notice the syntax <B>If Not<\/B>):<\/P><PRE class=\"codeSample\">If Not objUser.userAccountControl AND ADS_UF_DONT_EXPIRE_PASSWD Then\n<\/PRE>\n<P>Like we said earlier, the userAccountControl property is actually composed of a number of different property values; for the time being, think of them as being like switches on a control panel. All we\u2019re doing here is checking to see if the password expires switch (again, represented by the constant ADS_UF_DONT_EXPIRE_PASSWD) is on. If it is, then we\u2019re done. If it\u2019s not, then we need to turn it on. Bitwise logic is confusing, especially at first glance. But, at least in this case, the basic idea is simple: we take an individual switch (property) and either turn it on (enable it) or turn it off (disable it).<\/P>\n<P>And that\u2019s exactly what we do with the next two lines of code. In the first line we use the <B>Put<\/B> method to change the value of the userAccountControl in the local cache. (What\u2019s this local cache business all about? If you aren\u2019t sure, see this <A href=\"http:\/\/null\/technet\/scriptcenter\/funzone\/puzzle\/answers\/jan1306.mspx\"><B>Scripting Puzzle<\/B><\/A> answer for details.) Notice that we need to pass the Put method two parameters:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>userAccountControl<\/B>, the property we want to modify.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>objUser.userAccountControl XOR ADS_UF_DONT_EXPIRE_PASSWD<\/B>, bitwise code for &#8220;toggling&#8221; the current value of the \u201cpassword doesn\u2019t expire\u201d property. Essentially what we\u2019re saying here is this: if the switch (property) represented by the constant ADS_UF_DONT_EXPIRE_PASSWD is on then turn it off; if it\u2019s off then turn it on. This is easy to do, because the XOR operator simply toggles bitwise values on and off. That\u2019s why we had to make sure that Ken Myer\u2019s account currently did <I>not<\/I> have a password that never expires. If he already had a non-expiring password XOR would toggle that value, thus giving him a password that <I>did<\/I> expire.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>See? Every now and then we <I>do<\/I> have a reason for doing whatever it is we do.<\/P>\n<P>In the next line we call the <B>SetInfo<\/B> method to write the changes back to Active Directory. And with that, we\u2019re done.<\/P>\n<P>Of course, a lot of people might be more interested in going the other route; that is, taking an account where the password never expires and configuring it so that the password <I>does<\/I> expire. Here\u2019s a script that does just that, checking to see if the \u201cpassword never expires\u201d property has been enabled and, if so, disables it (again, using XOR to simply toggle the value):<\/P><PRE class=\"codeSample\">Const ADS_UF_DONT_EXPIRE_PASSWD = &amp;h10000<\/p>\n<p>Set objUser = GetObject(&#8220;LDAP:\/\/cn=Ken Myer, ou=Finance, dc=fabrikam, dc=com&#8221;)<\/p>\n<p>intUserAccountControl = objUser.Get(&#8220;userAccountControl&#8221;)<\/p>\n<p>If objUser.userAccountControl AND ADS_UF_DONT_EXPIRE_PASSWD Then\n    objUser.Put &#8220;userAccountControl&#8221;, _\n        objUser.userAccountControl XOR ADS_UF_DONT_EXPIRE_PASSWD\n    objUser.SetInfo\nEnd If\n<\/PRE>\n<P>So what do you say, BB: is that enough to get your vote that <I>Catch-22<\/I> is one of the four greatest novels ever written? We\u2019ll take that as a \u201cyes.\u201d<\/P>\n<TABLE id=\"EPH\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. In case you\u2019re wondering, the other three books on the list are \u2013 as everyone knows \u2013 <I>Sometimes a Great Notion<\/I>; <I>Night of the Hunter<\/I>; and <I>The Old Man and the Sea<\/I>. And, yes, we would put <I>Green Eggs and Ham<\/I> on there, too, except we don\u2019t think it\u2019s officially classified as a novel. But it\u2019s a pretty good book nonetheless.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I configure an Active Directory user account so that the account password never expires?&#8212; BB Hey, BB. Before we start we have a question for you: wouldn\u2019t you agree that Catch-22 is one of the four greatest novels ever written? We ask that because, in his English class, the Scripting [&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,9,3,4,20,5],"class_list":["post-66143","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-adsi","tag-scripting-guy","tag-scripting-techniques","tag-user-accounts","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I configure an Active Directory user account so that the account password never expires?&#8212; BB Hey, BB. Before we start we have a question for you: wouldn\u2019t you agree that Catch-22 is one of the four greatest novels ever written? We ask that because, in his English class, the Scripting [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66143","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=66143"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66143\/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=66143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}