{"id":54133,"date":"2009-03-23T21:26:00","date_gmt":"2009-03-23T21:26:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/03\/23\/hey-scripting-guy-how-can-i-create-the-same-user-account-and-password-on-multiple-computers\/"},"modified":"2009-03-23T21:26:00","modified_gmt":"2009-03-23T21:26:00","slug":"hey-scripting-guy-how-can-i-create-the-same-user-account-and-password-on-multiple-computers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-create-the-same-user-account-and-password-on-multiple-computers\/","title":{"rendered":"Hey, Scripting Guy! How Can I Create the Same User Account and Password on Multiple Computers?"},"content":{"rendered":"<h2><img decoding=\"async\" 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\" \/> <\/h2>\n<p>Hey, Scripting Guy! We have an application that runs on our local computers. It is a rather old application, and of course it is mission critical. Because this application is not Active Directory integrated, it means that I need to create a local user account on every computer that is going to run this particular application. All computers that run this application have the same user account created on them with the same password. I wish I could write a script that would create these accounts for me. Do you have any ideas?<\/p>\n<p>&#8211; ZT<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\" \/><img decoding=\"async\" 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\" \/> <\/p>\n<p>Hi ZT,<\/p>\n<p>We are hard at work on the 2009 Summer Scripting Games. We have been going back through literally thousands and thousands of old e-mails from the 2008 Winter Scripting Games. Our goal from this project is twofold. First we are trying to familiarize ourselves with all the things that went on last year so that we have a better feel for the way things were done and which things we can improve upon. Secondly, we have a poor old laptop that is sucking fumes in the resource department. When we open Office Outlook with all that e-mail, Outlook was consuming nearly half a gigabyte of memory. There was not much left in the way of resources. We am hoping by cleaning out the <b>scripter<\/b> inbox to reduce the memory footprint of my mission critical application\u2014Office Outlook.<\/p>\n<table id=\"E2C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">This week we will be looking at scripting Windows PowerShell as it applies to local account management. This is an area that comes up from time to time and for which there are not an awful lot of resources from which to choose. We have these tasks in the Script Center Script Repository pretty well hidden away in the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/ds\/local\/default.mspx\" target=\"_blank\">Other Directory Services<\/a> category. There are some great scripts in the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/csc\/scripts\/local\/default.mspx\" target=\"_blank\">Community-Submitted Scripts Center<\/a>. Local account management has been a favorite topic of the \u201cHey, Scripting Guy!\u201d articles over the years, and as a result we have a good selection of articles grouped together in the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/local.mspx\" target=\"_blank\">\u201cHey, Scripting Guy!\u201d archive<\/a>. The most extensive reference you will find is the MSDN coverage of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa772237(VS.85).aspx\" target=\"_blank\">WinNT ADSI provider<\/a>.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>We decided to write the <b>CreateLocalUser.ps1<\/b> script to create a local user account. You can find one that does a similar thing <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/ds\/local\/users\/lousvb03.mspx\" target=\"_blank\">using VBScript<\/a>.<\/p>\n<pre class=\"codeSample\"><pre class=\"codeSample\">$computer = [ADSI]\"WinNT:\/\/$env:ComputerName\"\n $user = $computer.Create(\"User\", \"MyTestUser\")\n $user.setpassword(\"MyPassword\")\n $user.put(\"description\",\"My description\") \n $user.SetInfo()\n<\/pre>\n<p>When we are talking about performing local account management, we are using the Computer Management tool seen in the following image. This tool can be found in the Administrative Tools section on your computer, but we prefer to launch it by typing <b>compmgmt.msc<\/b> in the <b>Start\/Run<\/b> box. On Windows Vista you must start the tool with administrative rights.<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Image of the Computer Management tool\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/march\/hey0323\/hsg-3-23-9-1.jpg\" width=\"500\" height=\"358\" \/> <\/p>\n<p>&nbsp;<\/p>\n<p>Though it is true that a local user can be created by right-clicking <b>Users<\/b> and clicking <b>New User<\/b> to display the dialog box seen in the following image, the ability to create a local user from a script adds a useful tool that can be readily used to ease part of the management burden.<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Image of the New User dialog box\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/march\/hey0323\/hsg-3-23-9-2.jpg\" width=\"384\" height=\"379\" \/> <\/p>\n<p>&nbsp;<\/p>\n<p>We do not even need to write a script if we do not want to do so. I can create a new local user account in one line of code from the Windows PowerShell console:<\/p>\n<pre class=\"codeSample\">PS C:\\&gt; (([adsi]\"WinNT:\/\/.\").Create(\"User\",\"MyTestUser\")).SetInfo()<\/pre>\n<p>We can also create a new local group in one line of code:<\/p>\n<pre class=\"codeSample\">PS C:\\&gt; (([adsi]\"WinNT:\/\/.\").Create(\"Group\",\"MyTestGroup\")).SetInfo()<\/pre>\n<p>The two lines of code are nearly identical. In fact, we used the up arrow to recall the previous command and changed the type of object to create and the name of the object. Let&#8217;s take a closer look at the syntax of the command. We start out with the <b>[adsi]<\/b> type accelerator. We then give it the ADSI provider we wish to use. In most examples, we use the LDAP provider because we are working with Active Directory. But because we are working with local user accounts, we need to use the WinNT provider.<\/p>\n<table id=\"E6E\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">The WinNT provider must be typed exactly like this: capital <b>W<\/b>, lower case <b>in<\/b>, and capital <b>NT<\/b>. All the ADSI providers are case sensitive. Remembering this can save you a lot of frustration later on if you are a sloppy typist.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>The <b>create user<\/b> command that creates a local user named <b>MyTestUser<\/b> is seen in Table 1.<\/p>\n<table id=\"EZF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead>\n<tr>\n<td class=\"tableHeader\" colSpan=\"7\">Table 1 Local ADSI command to create a user<\/td>\n<\/tr>\n<tr class=\"stdHeader\" vAlign=\"top\">\n<td id=\"colE3F\">[adsi]<\/td>\n<td id=\"colEAG\">WinNT<\/td>\n<td id=\"colEEG\">.<\/td>\n<td id=\"colEIG\">Create<\/td>\n<td id=\"colEMG\">User<\/td>\n<td id=\"colEQG\">MyTestUser<\/td>\n<td id=\"colEUG\">SetInfo<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">ADSI type accelerator<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">ADSI provider<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Computer Name<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Type of object<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Object name<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Method<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Now that we have some of the preliminaries out of the way, let&#8217;s take a detailed look at the <b>CreateLocalUser.ps1<\/b> script. We begin by using the <b>[adsi]<\/b> type accelerator. We feed the <b>[adsi]<\/b> type accelerator the ADSI provider we wish to use. We are using the WinNT provider because we are working with local user accounts. We do not need to tell the command the name of the computer if we do not wish to do so. We could use a period as we did earlier in the command line examples. I chose to include the computer name by reading it from the environmental PSDrive. We store the object that is returned in the <b>$computer<\/b> variable:<\/p>\n<pre class=\"codeSample\">$computer = [ADSI]\"WinNT:\/\/$env:ComputerName\"<\/pre>\n<p>Now we need to use the <b>Create<\/b> method so that we can create a new user. The <b>Create<\/b> method takes two arguments. The first argument is the type of object to create, and the second is the name we will give to the newly created object. We store the returned object in the <b>$user<\/b> variable as shown here:<\/p>\n<pre class=\"codeSample\">$user = $computer.Create(\"User\", \"MyTestUser\")<\/pre>\n<p>It is now time to specify the password for the user object. To do this, we use the <b>setpassword<\/b> method and give it the password for the user:<\/p>\n<pre class=\"codeSample\">$user.setpassword(\"MyPassword\")<\/pre>\n<p>We next decide to populate the <b>description<\/b> attribute. To do this, we use the <b>put<\/b> method. The <b>put<\/b> method accepts two arguments: the first is the attribute to modify and the second is the value to put into that attribute. This is illustrated here:<\/p>\n<pre class=\"codeSample\">$user.put(\"description\",\"My description\")<\/pre>\n<p>Finally we call the <b>SetInfo<\/b> method just like we do when working with ADSI and Active Directory. The <b>SetInfo<\/b> method commits the changes back to the directory. This is seen here:<\/p>\n<pre class=\"codeSample\">$user.SetInfo()<\/pre>\n<p>Well, ZT, that is about all there is to creating a local user account. We saw two methods for performing this feat; however, they use the same technique. Join us tomorrow as Local Account Management Week continues. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! We have an application that runs on our local computers. It is a rather old application, and of course it is mission critical. Because this application is not Active Directory integrated, it means that I need to create a local user account on every computer that is going to run this particular [&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":[197,23,24,3,198,45],"class_list":["post-54133","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-local-account-management","tag-local-accounts-and-windows-nt-4-0-accounts","tag-other-directory-services","tag-scripting-guy","tag-users","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! We have an application that runs on our local computers. It is a rather old application, and of course it is mission critical. Because this application is not Active Directory integrated, it means that I need to create a local user account on every computer that is going to run this particular [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54133","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=54133"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54133\/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=54133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=54133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=54133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}