{"id":64853,"date":"2007-05-16T22:56:00","date_gmt":"2007-05-16T22:56:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/05\/16\/how-can-i-force-all-the-users-in-an-ou-to-change-their-password-the-next-time-they-log-on\/"},"modified":"2007-05-16T22:56:00","modified_gmt":"2007-05-16T22:56:00","slug":"how-can-i-force-all-the-users-in-an-ou-to-change-their-password-the-next-time-they-log-on","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-force-all-the-users-in-an-ou-to-change-their-password-the-next-time-they-log-on\/","title":{"rendered":"How Can I Force All the Users in an OU to Change Their Password the Next Time They Log On?"},"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 write a script that will force all the users in an OU to change their password the next time they log on?<BR><BR>&#8212; TH<\/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, TH. You know, you picked a bad day to ask a question; the Scripting Guy who writes this column is in an even worse mood than usual (difficult as that might be to believe). A few weeks ago the Scripting Guys decided, what with the weather getting a bit better and the food in the Microsoft cafeteria getting a bit worse, that once a week they would \u2013 keep this under your hat, OK? \u2013 actually <I>leave the campus at lunch time<\/I> and go eat at a real restaurant. With that in mind, they bribed the guards, stole some uniforms from the Microsoft laundry, and dug a tunnel leading from their building across the street and into freedom.<\/P>\n<TABLE class=\"dataTable\" id=\"E6C\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. Wouldn\u2019t it be easier to just walk out the door and go to lunch at lunchtime? Oh, sure, <I>now<\/I> you tell us.<\/P>\n<P>And now that you mention it, Microsoft doesn\u2019t actually <I>have<\/I> guards. Wonder who that was we bribed?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>At any rate, today is supposed to be the day we go eat a real lunch. However, the Scripting Guys manager has called a team meeting right at the very moment the Scripting Guys had planned to enter the tunnel and make their escape. (It\u2019s important to go at just the right moment, when the searchlights are pointed in the opposite direction and the dogs are patrolling on the other side of the campus.) That means we\u2019re exchanging a real hamburger and fries for an update on team activities. Hurray!<\/P>\n<TABLE class=\"dataTable\" id=\"ETD\" 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>. Aren\u2019t team meetings an important way to get information, to foster morale, and to learn about ways to help us all do our jobs better? Hey, that\u2019s pretty funny; it\u2019s nice to see that <I>someone<\/I> has a sense of humor about all this.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>At any rate, the Scripting Guy who writes this column is in a bad mood. Originally he was going to register his displeasure by refusing to answer TH\u2019s question. However, that didn\u2019t seem right; after all, TH isn\u2019t the one who scheduled the team meeting for today. (Um, you\u2019re <I>not<\/I> the one who scheduled the meeting for today, are you, TH?) Consequently, the Scripting Guy who writes this column opted for Plan B: he would go on a hunger strike in order to protest having a team meeting today. However, that was before he discovered that you\u2019re not allowed to eat while on a hunger strike. (How crazy is <I>that<\/I>?) He then briefly considered going to Plan C, but coming up with yet another plan seemed like way too much work. Therefore, he decided to just answer TH\u2019s question and be done with it:<\/P><PRE class=\"codeSample\">Set objOU = GetObject(&#8220;LDAP:\/\/ou=Accounting,dc=fabrikam,dc=com&#8221;)\nobjOU.Filter = Array(&#8220;user&#8221;)<\/p>\n<p>For Each objUser in objOU\n    objUser.pwdLastSet = 0\n    objUser.SetInfo\nNext\n<\/PRE>\n<P>As you can see, this is a very simple little script. (In our defense, we said we\u2019d answer the question; we didn\u2019t say we\u2019d put an enormous amount of work into coming up with that answer.) To begin with, we connect to the desired OU in Active Directory; in our case, that happens to be the Accounting OU in the fabrikam.com domain:<\/P><PRE class=\"codeSample\">Set objOU = GetObject(&#8220;LDAP:\/\/ou=Accounting,dc=fabrikam,dc=com&#8221;)\n<\/PRE>\n<P>We actually get a lot of questions along these lines: how do I do something to all the user accounts\/computer accounts\/group accounts in an OU. People assume that this must be a very complicated task, one that requires both incredible coding skill and an intimate knowledge of Active Directory. Interestingly enough, this is actually about as <I>easy<\/I> a task as you\u2019ll ever do. When you bind to an OU in Active Directory you, by default, get back a collection of each and every item stored in that container. If we wanted to echo back the name of each of these items all we\u2019d have to do is set up a For Each loop and have at it:<\/P><PRE class=\"codeSample\">For Each objItem in objOU\n    Wscript.Echo objItem.Name\nNext\n<\/PRE>\n<P>Of course, we aren\u2019t interested in each and every item found in the Accounting OU; we\u2019re only interested in user accounts. Hence the second line of code in our script:<\/P><PRE class=\"codeSample\">objOU.Filter = Array(&#8220;user&#8221;)\n<\/PRE>\n<P>What we\u2019re doing here is applying a filter to our collection; in particular, we\u2019re saying that we want to filter out all the objects in the collection <I>except for<\/I> user objects. What if we were interested in working with computer accounts instead? Then we\u2019d use this filter:<\/P><PRE class=\"codeSample\">objOU.Filter = Array(&#8220;computer&#8221;)\n<\/PRE>\n<P>Pretty simple, huh? Note that you must always use an array when assigning object types to a filter, even if you only have one object in the filter. And if that makes you wonder, \u201cHmmm, does that mean we could put multiple objects in a single filter?\u201d the answer is: you bet you can. For example, here\u2019s a filter that weeds out everything except group accounts and printers (printQueue objects):<\/P><PRE class=\"codeSample\">objOU.Filter = Array(&#8220;group&#8221;, &#8220;printQueue&#8221;)\n<\/PRE>\n<P>Just separate the objects with commas and you can include as many items in the filter as your heart desires.<\/P>\n<P>As soon as our collection consists solely of user accounts we can go ahead and set up a For Each loop to cycle through entire set of items. Inside the loop we (automatically) bind to the first user account and then execute the following two lines of code:<\/P><PRE class=\"codeSample\">objUser.pwdLastSet = 0\nobjUser.SetInfo\n<\/PRE>\n<P>In line 1 we\u2019re assigning the value 0 to the <B>pwdLastSet<\/B> attribute. This attribute tells us when the user last set his or her password. If the value of pwdLastSet is 0 then the user must change his or her password the next time they log on. If the user is already logged on they\u2019ll be fine; they can continue working away without any problem. However, once they log off and then try to log back on they\u2019ll be forced to change their password.<\/P>\n<TABLE class=\"dataTable\" id=\"EEF\" 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>. Don\u2019t get too excited about the pwdLastSet attribute; it really <I>does<\/I> contain the date and time the password was last changed, but the date is stored \u2013 and we are not making this up \u2013 as a large integer that \u201crepresents the number of 100 nanosecond intervals since January 1, 1601 (UTC).\u201d If you\u2019d like to know the date and time a password was last changed take a peek at <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jul05\/hey0705.mspx\"><B>this <\/B><B><I>Hey, Scripting Guy!<\/I><\/B><B> column<\/B><\/A>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After setting the pwdLastSet attribute to 0 we then call the <B>SetInfo<\/B> method to write the change to the actual user account in Active Directory. We then loop around and repeat the process with the next user in the collection. When we\u2019re all done, each and every user in the Accounting OU will be required to change their password the next time they log on.<\/P>\n<TABLE class=\"dataTable\" id=\"EBG\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. OK, that might not be entirely true: this approach works only if the user has a password that expires. If any of your users have non-expiring passwords (which, we might add, is definitely <I>not<\/I> recommended) then setting pwdLastSet to 0 will have no effect on their account. But don\u2019t worry: we just happen to have <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/oct06\/hey1031.mspx\"><B>yet another <\/B><B><I>Hey, Scripting Guy!<\/I><\/B><B> column<\/B><\/A> that shows you how to identify non-expiring passwords, and even how to turn them into expiring passwords.<\/P>\n<P>Let\u2019s face it: after 3+ years we have a <I>lot<\/I> of <I>Hey, Scripting Guy!<\/I> columns.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In case you\u2019re wondering, the food at the Microsoft cafeteria really isn\u2019t all that bad; in fact, the only problem we have with it is that they have the philosophy, \u201cIf you liked what we served you on Monday you\u2019ll like it even better on Tuesday. And by Friday, you\u2019ll love it!\u201d Nevertheless, having yet <I>another<\/I> gyro sandwich is still probably better than crawling a mile or so through a dank and dark tunnel.<\/P>\n<P>Probably.<\/P>\n<TABLE class=\"dataTable\" id=\"EHH\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>P.S<\/B>. For once this column has a happy ending: the team meeting ran extremely short, and the Scripting Guys managed to go out to lunch after all!<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I write a script that will force all the users in an OU to change their password the next time they log on?&#8212; TH Hey, TH. You know, you picked a bad day to ask a question; the Scripting Guy who writes this column is in an even worse mood [&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-64853","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 write a script that will force all the users in an OU to change their password the next time they log on?&#8212; TH Hey, TH. You know, you picked a bad day to ask a question; the Scripting Guy who writes this column is in an even worse mood [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64853","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=64853"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64853\/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=64853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}