{"id":65143,"date":"2007-04-05T20:50:00","date_gmt":"2007-04-05T20:50:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/04\/05\/how-can-i-configure-an-expiration-date-for-a-local-user-account\/"},"modified":"2007-04-05T20:50:00","modified_gmt":"2007-04-05T20:50:00","slug":"how-can-i-configure-an-expiration-date-for-a-local-user-account","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-configure-an-expiration-date-for-a-local-user-account\/","title":{"rendered":"How Can I Configure an Expiration Date for a Local User Account?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I configure an expiration date for a local user account?<BR><BR>&#8212; GW<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, GW. You know, it\u2019s amazing how quickly things change in the fast-paced world of system administration scripting. For example, in Monday\u2019s column we announced that, as time allowed and whenever possible, the Scripting Guys would include a Windows PowerShell solution in <I>Hey, Scripting Guy!<\/I> along with the VBScript solutions we typically provide people. Today, just a few days later, we\u2019re making a new announcement: the Scripting Guys aren\u2019t going to provide Windows PowerShell solutions after all.<\/P>\n<P>Whoa, whoa settle down; let us finish. It\u2019s true that the <I>Scripting Guys<\/I> aren\u2019t going to provide Windows PowerShell solutions; instead we have something much better in mind: June Blender, PowerShell writer to the stars, is going to provide those solutions for us. That\u2019s better for all of us, and for at least two reasons. First, June actually knows what she\u2019s doing. Second, if June is doing the work then the Scripting Guy who writes this column doesn\u2019t have to do anything at all. That part we <I>really<\/I> like.<\/P>\n<TABLE class=\"dataTable\" id=\"EHD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Good question: is June Blender <I>really<\/I> the PowerShell writer to the stars? To be honest, she denies that. But rumor has it that any time Tom Hanks or Cate Blanchett need a PowerShell script they contact June. And no, to the best of our knowledge, both Meryl Streep and Brad Pitt still write their own PowerShell scripts.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Admittedly, there is one drawback to this new arrangement: the fact that the Scripting Guy who writes this column typically writes it the day before it\u2019s due to appear in the Script Center. As a matter of fact, it\u2019s often a race to see if he can finish the column before it\u2019s time for him to go home. (What\u2019s that? Stay a little later so he can finish the column and <I>then<\/I> go home? You don\u2019t know much about the Scripting Guy who writes this column, do you?) That means that the Windows PowerShell solutions will usually be added after the fact: we\u2019ll publish a column on, say, Thursday, then add the PowerShell stuff on Friday. We\u2019ll see if we can come up with a better way of syncing that information, but we can\u2019t make any promises. However, we <I>will<\/I> promise to do something somewhere that will help you locate the articles that include both a VBScript solution and a Windows PowerShell solution.<\/P>\n<P>In fact, June has already started adding her PowerShell solutions. Don\u2019t believe us? Then take a peek at <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/mar07\/hey0329.mspx\"><B>this column<\/B><\/A>. Oh, and at <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr07\/hey0402.mspx\"><B>this column<\/B><\/A>, too. And <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jul06\/hey0714.mspx\"><B>this one<\/B><\/A>. Incidentally, that last column doesn\u2019t have any Windows PowerShell stuff in it; we just thought it was a good column.<\/P>\n<P>Needless to say, the average daily scripting columnist would break June in gently, probably by choosing a question that can easily be addressed using Windows PowerShell. But the Scripting Guy who writes this column considers himself <I>anything<\/I> but average; therefore, we\u2019re going to start off by showing you how to set the account expiration date for a local user account:<\/P><PRE class=\"codeSample\">Set objUser = GetObject(&#8220;WinNT:\/\/atl-ws-01\/kenmyer&#8221;)<\/p>\n<p>objUser.AccountExpirationDate = #03\/01\/2008# \nobjUser.SetInfo\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EXE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Note to Windows Vista users: This script works only if you Run As Administrator. When you open the Command Prompt, right-click and select Run As Administrator, then run the script from that command prompt window.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As you can see, there\u2019s hardly anything to this script, at least not the VBScript version. (What about the Windows PowerShell version? Beats us; after all, that\u2019s not <I>our<\/I> problem.) We start off by binding to the kenmyer user account on the computer atl-ws-01. Once we\u2019ve made the connection we then set the value of the <B>AccountExpirationDate<\/B> attribute to March 1, 2008:<\/P><PRE class=\"codeSample\">objUser.AccountExpirationDate = #03\/01\/2008#\n<\/PRE>\n<P>Why the little pound signs (#) around the date? That\u2019s actually optional; we could have enclosed the date in double quote marks instead. However, in VBScript the pound signs specify that the value in question is a date-time value. It\u2019s a good idea (dare we say a \u201cbest practice?\u201d) to enclose your date-time values in pound signs; that helps guard against VBScript mistakenly viewing the value as a string, or even as a mathematical equation (e.g., 3 divided by 1 divided by 2008).<\/P>\n<P>After assigning a value to the AccountExpirationDate attribute we then call the <B>SetInfo<\/B> method and officially write the change to the System Account Manager (SAM) on atl-ws-01. For those of you unfamiliar with the method, SetInfo is roughly equivalent to the Save command in an application such as Microsoft Word. You can type as much stuff into a Word document as you want, but if you never save the document then all that effort is for naught. The same is true when you use ADSI to modify attribute values. We can assign any value we want to AccountExpirationDate (or any other attribute, for that matter), but those changes do not take effect until we call SaveInfo. If the script ends without us calling SaveInfo then those changes will <I>never<\/I> take effect.<\/P>\n<P>And sure, you can get a little fancy here. For example, suppose you want to give Ken Myer a temporary, 30-day account on atl-ws-01. Here\u2019s a script that sets the expiration date to the current date plus 30 days:<\/P><PRE class=\"codeSample\">Set objUser = GetObject(&#8220;WinNT:\/\/atl-ws-01\/kenmyer&#8221;)<\/p>\n<p>objUser.AccountExpirationDate = Date + 30 \nobjUser.SetInfo\n<\/PRE>\n<P>Well, we did say a <I>little<\/I> fancy. But you get the idea.<\/P>\n<P>Of course, if you\u2019re going to go around changing account expiration dates that means that, sooner or later, you\u2019ll need to verify those expiration dates for all the local accounts on atl-ws-01. Good thing we had this verify-account-expiration-dates script lying around, isn\u2019t it:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Set objAccounts = GetObject(&#8220;WinNT:\/\/atl-ws-01&#8221;)\nobjAccounts.Filter = Array(&#8220;User&#8221;)<\/p>\n<p>For Each objUser In objAccounts\n   If IsNull(objUser.AccountExpirationDate) Then\n       Wscript.Echo objUser.Name, &#8220;Account has no expiration date.&#8221;\n   Else\n       Wscript.Echo objUser.Name, objUser.AccountExpirationDate\n   End If\nNext\n<\/PRE>\n<P>All we\u2019re doing here is connecting to the SAM on atl-ws-01, then applying a <B>Filter<\/B> to ensure that the only items we get back are user accounts. (Otherwise we\u2019d get back groups and printers and services and everything else known to the SAM.) After applying the filter we set up a For Each loop to loop through the entire collection of user accounts; inside that loop we use the <B>IsNull<\/B> function to identify accounts that don\u2019t <I>have<\/I> an expiration date:<\/P><PRE class=\"codeSample\">If IsNull(objUser.AccountExpirationDate) Then\n<\/PRE>\n<P>If AccountExpirationDate is null we echo back a message to that effect; otherwise we echo back the user name and the account expiration date:<\/P><PRE class=\"codeSample\">Wscript.Echo objUser.Name, objUser.AccountExpirationDate\n<\/PRE>\n<P>The end result is something that looks like this:<\/P><PRE class=\"codeSample\">Administrator Account has no expiration date.\nASPNET Account has no expiration date.\nGuest Account has no expiration date.\nHelpAssistant Account has no expiration date.\nkenmyer 3\/1\/2008\npilarackerman 6\/1\/2007\n<\/PRE>\n<P>So much for the VBScript version. As for the Windows PowerShell version, well, we\u2019ll let June take care of that. <\/P>\n<P>You know, we just thought of something: if we could find someone to write the <I>VBScript<\/I> version for us, well \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I configure an expiration date for a local user account?&#8212; GW Hey, GW. You know, it\u2019s amazing how quickly things change in the fast-paced world of system administration scripting. For example, in Monday\u2019s column we announced that, as time allowed and whenever possible, the Scripting Guys would include a Windows [&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":[23,24,3,5],"class_list":["post-65143","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-local-accounts-and-windows-nt-4-0-accounts","tag-other-directory-services","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I configure an expiration date for a local user account?&#8212; GW Hey, GW. You know, it\u2019s amazing how quickly things change in the fast-paced world of system administration scripting. For example, in Monday\u2019s column we announced that, as time allowed and whenever possible, the Scripting Guys would include a Windows [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65143","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=65143"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65143\/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=65143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}