{"id":69463,"date":"2005-07-05T20:58:00","date_gmt":"2005-07-05T20:58:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/07\/05\/how-can-i-determine-when-a-user-last-changed-his-or-her-password\/"},"modified":"2005-07-05T20:58:00","modified_gmt":"2005-07-05T20:58:00","slug":"how-can-i-determine-when-a-user-last-changed-his-or-her-password","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-when-a-user-last-changed-his-or-her-password\/","title":{"rendered":"How Can I Determine When a User Last Changed His or Her Password?"},"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 determine when a user last changed his or her password?<BR><BR>&#8212; MG<\/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, MG. A long time ago one of the Scripting Guys worked at a local university. In one of the departments there it was easy to determine when a user last changed his or her password: that\u2019s because the administrative assistant kept a list of all the users in the department, all their passwords, and the dates and times when those passwords were changed. Best of all, she kept that list in a folder out on her desk; that way if anyone ever forgot their password they could just grab the folder and look up their password. <\/P>\n<P>And, yes, anyone else\u2019s password as well.<\/P>\n<P>So that\u2019s <I>one<\/I> way to determine the last time a user changed his or her password. But here\u2019s another way:<\/P><PRE class=\"codeSample\">Set objUser = GetObject(&#8220;LDAP:\/\/CN=myerken,OU=management,DC=Fabrikam,DC=com&#8221;)\nWscript.Echo &#8220;Password last changed: &#8221; &amp; objUser.PasswordLastChanged\n<\/PRE>\n<P>Yes, that <I>is<\/I> the entire script; this isn\u2019t one of those Web sites where we just show you the beginning of something and then make you pay to see the rest. (Although now that we think about it\u2026.) If we want to know the last time Ken Myer changed his password we just need to do two things:<\/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>Bind to Ken\u2019s user account in Active Directory.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Echo the value of the <B>PasswordLastChanged <\/B>attribute.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>That\u2019s all you have to do. Really.<\/P>\n<P>Of course, we\u2019re assuming that you\u2019re talking about an Active Directory user account; it\u2019s quite possible that you\u2019re talking about a Windows NT 4.0 user account, or even a local user account. If so, we appear to have a bit of a problem; after all, you need to use the WinNT provider to access NT 4.0 or local user accounts, and the WinNT provider does not support the PasswordLastChanged attribute. <\/P>\n<P>So does that mean we\u2019re out of luck when it comes to NT 4.0 or local user accounts? Ah, you guys should know us better than that:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ws-01&#8221;<\/p>\n<p>Set objUser = GetObject(&#8220;WinNT:\/\/&#8221; &amp; strComputer &amp; &#8220;\/kenmyer&#8221;)<\/p>\n<p>intPasswordAge = objUser.PasswordAge\nintPasswordAge = intPasswordAge * -1 \ndtmChangeDate = DateAdd(&#8220;s&#8221;, intPasswordAge, Now)<\/p>\n<p>WScript.Echo &#8220;Password last changed: &#8221; &amp; dtmChangeDate\n<\/PRE>\n<P>As you can see, this script is a tiny bit longer, but still pretty simple. We begin by connecting to the Ken Myer account (username <B>kenmyer<\/B>) on the computer atl-ws-01. (What if you wanted to connect to an NT 4.0 domain instead? That\u2019s fine; just replace the value <B>atl-ws-01<\/B> with the name of that domain.) We then grab the value of the <B>PasswordAge<\/B> attribute and store it in a variable named intPasswordAge.<\/P>\n<P>How come we don\u2019t just echo the value of PasswordAge? Well, PasswordAge actually represents the number of seconds that have expired since the password last changed. Suppose you get back an answer like this:<\/P><PRE class=\"codeSample\">50725249\n<\/PRE>\n<P>Off the top of your head, can you subtract 50725249 seconds from the current date and time and calculate when the password was last changed?<\/P>\n<P>Really? Wow; that\u2019s impressive. We couldn\u2019t do that ourselves, however, so we let the script do it for us. To begin with, we took the value of intPasswordAge and multiplied it by -1:<\/P><PRE class=\"codeSample\">intPasswordAge = intPasswordAge * -1\n<\/PRE>\n<P>Why? Well, in the very next line of code we\u2019re going to use the <B>DateAdd<\/B> function to determine the date the password was last set. To do that, we need to pass DateAdd three 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>The time interval we\u2019re dealing with. Because PasswordAge is stored in seconds we use <B>\u201cs\u201d<\/B> as our first parameter.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The number of seconds to add to the current date. This is why we multiplied intPasswordAge by -1. We don\u2019t really want to <I>add<\/I> intPasswordAge to the current date; that would give us a time somewhere out in the future. Instead, we want to <I>subtract<\/I> intPasswordAge from the current date; after all, intPasswordAge is the number of seconds that have elapsed since the password was last changed. Because adding a negative number is the same thing as subtracting a positive number, we multiply intPasswordAge by -1.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The current date and time. For that we use the VBScript function <B>Now<\/B>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>That\u2019s it; the result of our equation is stored in the variable dtmChangeDate, and in the last line of code we echo the value of that variable. At the time we created and tested this script it told us the password was last changed on 11\/22\/2003 at 12:02:10 PM. (It also told us that we don\u2019t do a very good job of keeping our local user account passwords up to date.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine when a user last changed his or her password?&#8212; MG Hey, MG. A long time ago one of the Scripting Guys worked at a local university. In one of the departments there it was easy to determine when a user last changed his or her password: that\u2019s because [&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,23,24,3,20,5],"class_list":["post-69463","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-local-accounts-and-windows-nt-4-0-accounts","tag-other-directory-services","tag-scripting-guy","tag-user-accounts","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I determine when a user last changed his or her password?&#8212; MG Hey, MG. A long time ago one of the Scripting Guys worked at a local university. In one of the departments there it was easy to determine when a user last changed his or her password: that\u2019s because [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69463","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=69463"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69463\/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=69463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}