{"id":65843,"date":"2006-12-14T16:03:00","date_gmt":"2006-12-14T16:03:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/12\/14\/how-can-i-set-a-users-password-and-then-require-them-to-change-that-password-the-next-time-they-log-on\/"},"modified":"2006-12-14T16:03:00","modified_gmt":"2006-12-14T16:03:00","slug":"how-can-i-set-a-users-password-and-then-require-them-to-change-that-password-the-next-time-they-log-on","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-set-a-users-password-and-then-require-them-to-change-that-password-the-next-time-they-log-on\/","title":{"rendered":"How Can I Set a User&#8217;s Password and Then Require Them to Change That Password the Next Time They Log on?"},"content":{"rendered":"<p><img decoding=\"async\" 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\"><\/p>\n<p>Hey, Scripting Guy! In our school, how can I enable teachers to change a student\u2019s password to their logon name, then force those students to change their password the next time they log on?<\/p>\n<p>&#8212; DS<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><img decoding=\"async\" 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 decoding=\"async\" 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><\/p>\n<p>Hey, DS. Boy, did you pick the wrong person to ask. After all, the Scripting Guy who writes this column used to be a teacher; if he knew how to force students to do things he\u2019d probably still be teacher!<\/p>\n<p>Actually, the Scripting Guy who writes this column always got along really well with his students; in fact, he\u2019s always gotten along really well with kids, period. As his fellow Scripting Guys will attest, it\u2019s only <i>adults<\/i> he has problems with. Perhaps that\u2019s because, as the Scripting Son pointed out a few weeks ago, \u201cYou know, any place you go you\u2019re usually the most immature person there.\u201d And no, we have <i>no idea<\/i> what could have provoked this outburst.<\/p>\n<p>Well, OK, maybe we have <i>some<\/i> idea \u2026.<\/p>\n<p>But regardless of whether you\u2019re dealing with a grownup, a child, or even a Scripting Guy, here\u2019s a script that will set a user\u2019s password to their logon name, then require that user to change that password the next time they logon:<\/p>\n<pre class=\"codeSample\">Set objUser = GetObject(\"LDAP:\/\/cn=Ken Myer,ou=Finance,dc=fabrikam,dc=com\")\nobjUser.SetPassword objUser.sAMAccountName\nobjUser.Put \"pwdLastSet\", 0\nobjUser.SetInfo\n<\/pre>\n<p>And yes, it <i>is<\/i> a very short and concise script, isn\u2019t it? Yet another reason why the Scripting Guy who writes this column is no longer a teacher: teachers have to work hard. As for Scripting Guys, well \u2026.<\/p>\n<p>As you can see, we start out by binding to the user account in Active Directory; that\u2019s what this line of code does:<\/p>\n<pre class=\"codeSample\">Set objUser = GetObject(\"LDAP:\/\/cn=Ken Myer,ou=Finance,dc=fabrikam,dc=com\")\n<\/pre>\n<p>We then call the <b>SetPassword<\/b> method in order to change the user\u2019s password. With SetPassword we have to supply one parameter: the new password for the user. Because we want the new password to be the user\u2019s logon name we pass the value of the <b>sAMAccountName<\/b> attribute: <\/p>\n<pre class=\"codeSample\">objUser.SetPassword objUser.sAMAccountName\n<\/pre>\n<p>Believe it or not, that\u2019s all we have to do to set the user\u2019s password.<\/p>\n<p>Requiring the user to change that password the next time they log on is twice as hard; after all, that requires two whole lines of code:<\/p>\n<pre class=\"codeSample\">objUser.Put \"pwdLastSet\", 0\nobjUser.SetInfo\n<\/pre>\n<p>In the first line we assign the value 0 to the <b>pwdLastSet<\/b> attribute. This attribute keeps track of the last time a user changed his or her password. If the value is set to 0 then the user has to change their password the next time they log on.<\/p>\n<p>In line 2 we then call the <b>SetInfo<\/b> method to write the updated attribute value back to Active Directory. <\/p>\n<p>That should do the trick, DS, provided, of course, that teachers have the right to change user passwords and to update the pwdLastSet attribute.<\/p>\n<p>Speaking of teachers, the Scripting Guy who writes this column actually enjoyed teaching; that\u2019s one reason he spends so much of his time coaching. What he <i>didn\u2019t<\/i> like were school districts: huge, impersonal bureaucracies that never know what they\u2019re doing, or why. And so he ended up at Microsoft instead.<\/p>\n<p>And yes, we know what you\u2019re thinking. But if anyone asks, you said it, not us.<\/p>\n<p><br><\/p>\n<div>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<tbody>\n<tr>\n<td class=\"\"><a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/dec06\/hey1214.mspx#top\"><img decoding=\"async\" height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/a><a class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/dec06\/hey1214.mspx#top\">Top of page<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! In our school, how can I enable teachers to change a student\u2019s password to their logon name, then force those students to change their password the next time they log on? &#8212; DS Hey, DS. Boy, did you pick the wrong person to ask. After all, the Scripting Guy who writes this [&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":[421,3,63,5],"class_list":["post-65843","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-passwords","tag-scripting-guy","tag-security","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! In our school, how can I enable teachers to change a student\u2019s password to their logon name, then force those students to change their password the next time they log on? &#8212; DS Hey, DS. Boy, did you pick the wrong person to ask. After all, the Scripting Guy who writes this [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65843","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=65843"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65843\/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=65843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}