{"id":71213,"date":"2004-10-15T17:57:00","date_gmt":"2004-10-15T17:57:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/10\/15\/how-can-i-change-a-users-password\/"},"modified":"2004-10-15T17:57:00","modified_gmt":"2004-10-15T17:57:00","slug":"how-can-i-change-a-users-password","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-change-a-users-password\/","title":{"rendered":"How Can I Change a User\u2019s Password?"},"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 change a user\u2019s password using a script?<BR><BR>&#8212; GO<\/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, GO. You didn\u2019t indicate whether you wanted to change the password for a local user or for an Active Directory user. But that\u2019s OK: the processes are so similar we\u2019ll go ahead and show you how to do both. It\u2019s like getting two <I>Hey, Scripting Guy!<\/I> columns for the price of one.<\/P>\n<P>Regardless of whether you want to change a local user password or an Active Directory user password you need to go through a two-step process. First you bind to the user account in question, and then you use ADSI\u2019s SetPassword method to assign the user a new password. That\u2019s it: two steps and you\u2019re done.<\/P>\n<P>To prove it, let\u2019s start by changing the password for a local user. In the following script, we bind to the kenmyer user account on the computer atl-ws-01 and assign Ken the password i5a2sj*!:<\/P><PRE class=\"codeSample\">Set objUser = GetObject(&#8220;WinNT:\/\/atl-ws-01\/kenmyer&#8221;)\nobjUser.SetPassword(&#8220;i5A2sj*!&#8221;)\n<\/PRE>\n<P>That\u2019s the whole script right there: bind to the user account, call the SetPassword method, passing SetPassword the user\u2019s new password. The only thing to watch out for is the way you format the provider name. It has to be <B>WinNT<\/B>, with the W and the NT in uppercase letters. Write that out in any other way &#8211; for example, winnt &#8211; and the script will fail. This is one of the very rare times in which case-sensitivity is important in VBScript. Other than that, there\u2019s nothing to it.<\/P>\n<P>Of course, you might be thinking, \u201cYeah, they start out with a local user script because local user accounts are so simple compared to Active Directory user accounts. Just wait until they try to change the password for an Active Directory user account.\u201d Well, the waiting is over; here\u2019s a script that changes the password for the kenmyer user account in the domain fabrikam.com:<\/P><PRE class=\"codeSample\">Set objUser = GetObject(&#8220;LDAP:\/\/cn=KenMyer,ou=Finance,dc=fabrikam,dc=com&#8221;)\nobjUser.SetPassword(&#8220;i5A2sj*!&#8221;)\n<\/PRE>\n<P>That\u2019s right: it\u2019s remarkably similar to the script for changing a local user password. The only difference is that we use the LDAP provider to bind to the user account (the LDAP provider is used when working with Active Directory and the WinNT provider is used when working with local accounts and Windows NT 4.0 domains). And, of course, the path to the actual account will vary depending on whether the account is stored locally or in Active Directory. But other than that the two scripts are identical.<\/P>\n<P>We should mention that you can change <I>any<\/I> user account with these scripts, including the local Administrator account. Just replace <B>kenmyer<\/B> with <B>Administrator<\/B>:<\/P><PRE class=\"codeSample\">Set objUser = GetObject(&#8220;WinNT:\/\/atl-ws-01\/Administrator&#8221;)\nobjUser.SetPassword(&#8220;i5A2sj*!&#8221;)\n<\/PRE>\n<P>In fact, as long as we\u2019re at it, let\u2019s give you three columns for the price of one. A question we get asked all the time is this: How do I change the local Administrator password for all the computers in an OU? Well, here\u2019s your answer:<\/P><PRE class=\"codeSample\">Set objOU = GetObject(&#8220;LDAP:\/\/OU=Finance, DC=fabrikam, DC=com&#8221;)\nobjOU.Filter = Array(&#8220;Computer&#8221;)<\/p>\n<p>For Each objItem in objOU\n    strComputer = objItem.CN\n    Set objUser = GetObject(&#8220;WinNT:\/\/&#8221; &amp; strComputer &amp; &#8220;\/Administrator&#8221;)\n    objUser.SetPassword(&#8220;i5A2sj*!&#8221;)\nNext\n<\/PRE>\n<P>So what are we doing in <I>this<\/I> script? Well, we\u2019re binding to the Finance OU in fabrikam.com. We\u2019re then applying a filter to the collection we get back to make sure that we\u2019re dealing only with computer accounts. After we\u2019ve applied the filter, we loop through the collection of computer accounts. We grab the CN (essentially the NetBIOS name) for the first computer and store the name in the variable strComputer. We then connect to the Administrator account on that machine and change the password. The script loops around and repeats the process for the second computer in the collection, and then continues looping around and changing passwords until it\u2019s hit every computer in the OU.<\/P>\n<P>We know. But we won\u2019t tell your boss how easy this is. Let him or her think you\u2019re a real genius when you tell them you\u2019ve discovered a way to automatically change all the local Administrator passwords on all the computers in an OU. The fact that it really takes just a few simple lines of code will be our little secret. (Trust us: we\u2019ve never told <I>our<\/I> bosses how easy this is, either!)<\/P><BR>\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\/oct04\/hey1015.mspx#top\"><IMG 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\/oct04\/hey1015.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I change a user\u2019s password using a script?&#8212; GO Hey, GO. You didn\u2019t indicate whether you wanted to change the password for a local user or for an Active Directory user. But that\u2019s OK: the processes are so similar we\u2019ll go ahead and show you how to do both. It\u2019s [&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-71213","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 change a user\u2019s password using a script?&#8212; GO Hey, GO. You didn\u2019t indicate whether you wanted to change the password for a local user or for an Active Directory user. But that\u2019s OK: the processes are so similar we\u2019ll go ahead and show you how to do both. It\u2019s [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71213","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=71213"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71213\/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=71213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}