{"id":11001,"date":"2012-02-29T00:01:00","date_gmt":"2012-02-29T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/02\/29\/use-powershell-to-replace-netdom-commands-to-join-the-domain\/"},"modified":"2012-02-29T00:01:00","modified_gmt":"2012-02-29T00:01:00","slug":"use-powershell-to-replace-netdom-commands-to-join-the-domain","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-replace-netdom-commands-to-join-the-domain\/","title":{"rendered":"Use PowerShell to Replace netdom Commands to Join the Domain"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to replace <em>netdom<\/em> commands with simple Windows PowerShell cmdlets to rename and reboot the computer or join the domain.<\/p>\n<p><span><span><span><span><span><span><span><span><img decoding=\"async\" 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\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>&nbsp;Hey, Scripting Guy! It seems that I have been hand building a number of computers recently for a computer lab we are setting up at work. I have written a batch file that uses <i>netdom <\/i>commands to<i> <\/i>join the domain. I also use a <i>netdom <\/i>command to rename the computer, and the <i>shutdown <\/i>command to restart the computer. The syntax for each of these three commands is rather complex and convoluted. A strange thing is that it seems I can do this on Windows Server&nbsp;R2, but I cannot do this on Windows&nbsp;7. What gives?<\/p>\n<p>&mdash;AD<\/p>\n<p><span><span><span><span><span><span><span><span><img decoding=\"async\" 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\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>&nbsp;Hello AD,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Well this afternoon I am drinking something a bit different. I decided to make a cup of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Chai\" target=\"_blank\">masala chai<\/a>. (The word <i>chai, <\/i>or many of its variations, simply means tea in many languages. Therefore, to speak of chai tea is redundant.) Anyway, I decided to use Dajarling tea, brewed a little strong, and I added cloves, cardamom, a cinnamon stick, fresh ground pepper, and 1\/3 cup of warm milk. Coupled with an <a href=\"http:\/\/en.wikipedia.org\/wiki\/Anzac_biscuit\" target=\"_blank\">Anzac biscuit<\/a>, it was quite nice.<\/p>\n<p>AD, the reason that you cannot use your batch file (containing <i>netdom <\/i>commands) on Windows&nbsp;7 is that by default Windows&nbsp;7 does not contain the <i>netdom <\/i>command. You can add <i>netdom <\/i>to your computer running Windows&nbsp;7 by installing the latest version of the <a href=\"http:\/\/www.microsoft.com\/download\/en\/details.aspx?displaylang=en&amp;id=7887\" target=\"_blank\">Remote Server Administration Tools (RSAT)<\/a>. When it is installed, you still need to go to Programs and Features and turn on the tools you want to load. The RSAT tools are great, and that is where you gain access to the Active Directory module. But you should not load the RSAT only to access <i>netdom, <\/i>because you can do what you want to accomplish out of the box (assuming that your box is not Windows&nbsp;7 Home edition that does not join domains).<\/p>\n<p>AD, your batch file contained at least three commands to rename the computer, join the domain, and to restart the machine. The two <i>netdom <\/i>commands and the <i>shutdown <\/i>command are shown here.<\/p>\n<p style=\"padding-left: 30px\">netdom renamecomputer member \/newname:member1.example.com \/userd:administrator<\/p>\n<p style=\"padding-left: 30px\">netdom add \/d:reskita mywksta \/ud:mydomain\\admin \/pd:password<\/p>\n<p style=\"padding-left: 30px\">shutdown \/r<\/p>\n<p>In Windows PowerShell&nbsp;2.0, this is still three commands, but at least the commands are native to Windows&nbsp;7. In addition, the Windows PowerShell command is easier to read, and they support prototyping. An example of using Windows PowerShell to add a computer to the domain, rename the computer, and reboot the machine is shown here.<\/p>\n<p style=\"padding-left: 30px\">(Get-WmiObject win32_computersystem).rename(&#8220;newname&#8221;)<\/p>\n<p style=\"padding-left: 30px\">add-computer -Credential iammred\\administrator -DomainName iammred.net<\/p>\n<p style=\"padding-left: 30px\">Restart-Computer<\/p>\n<p>In the first command, I use the <b>Get-WmiObject<\/b> cmdlet to retrieve the &lt;a href=&quot;http:\/\/blogs.technet.com\/controlpanel\/blogs\/posteditor.aspx\/<a href=\"\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa394102(v=vs.85).aspx&quot;\">Win32_ComputerSystem<\/a>&#8221; target=&#8221;_blank&#8221;&gt;Win32_ComputerSystem <\/a>Windows Management Instrumentation class. (The <b>Get-WmiObject<\/b> cmdlet has an alias of <b>gwmi<\/b>, and it will also take credentials if required.) Because this class returns only one instance, I can use my <i>group <\/i>and <i>dot <\/i>trick (see <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/06\/18\/top-ten-favorite-powershell-tricks-part-1.aspx\" target=\"_blank\">My Ten Favorite Windows PowerShell Tricks<\/a>) to directly call the <b>Rename<\/b><i> <\/i>method to rename the computer.<\/p>\n<p>After I rename the computer, I use the <b>Add-Computer <\/b>cmdlet to join the computer to the domain. The <b>Add-Computer <\/b>cmdlet allows me to specify the credentials that have rights to add computers to the domain, in addition to the name of the domain to join. Although I did not do it in my example, there is also an <i>ou <\/i>parameter that allows you to specify the path to the OU that will contain the newly created computer account.<\/p>\n<p>The last command, <b>Restart-Computer<\/b>,<b> <\/b>appears without any parameters. This means that the computer will restart within one minute, and it will attempt to cause processes to politely exit (generally a good thing). For emergency type of situations, there is the <i>Force <\/i>switch that will cause the computer to immediately restart, and not wait on processes to politely exit. The use of this optional parameter can lead to data loss in some situations.<\/p>\n<p>In the image that follows, I first use the <b>Get-WmiObject <\/b>cmdlet to rename the computer. The image shows the return value is 0, which means that the command completed successfully. Next, I use the <b>Add-Computer <\/b>cmdlet to join the computer to the <i>iammred <\/i>domain by using the administrator credentials. Upon hitting ENTER, a dialog box appears that requests the password for the credentials.<\/p>\n<p>The command completed successfully, but a warning message states that a reboot is required for the change to actually take place. The last command shown in the image uses the <b>Restart-Computer <\/b>cmdlet to restart the computer. I added the <i>WhatIf <\/i>parameter to illustrate what happens when using the <i>WhatIf <\/i>parameter (and to permit myself time to make the screenshot).<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4572.hsg-2-29-12-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4572.hsg-2-29-12-1.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>After I remove the <i>WhatIf <\/i>switch, and rerun the <b>Restart-Computer <\/b>cmdlet, a message box appears that states the computer will shut down in a minute or less. After the quick reboot, I am able to switch from using a local account to a domain account, because the computer has now joined the domain. The commands are short, sweet, easy to remember, and easy to use. None of these commands require a script, in fact, they could easily be run as imported history commands. For more information about working with the Windows PowerShell history cmdlets, see <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=history&amp;sections=7618\" target=\"_blank\">this collection of Hey, Scripting Guy! blogs<\/a>.<\/p>\n<p>&nbsp;AD, that is all there is to using Windows PowerShell to rename a computer and to join it to the domain.&nbsp; Join me tomorrow for more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to replace netdom commands with simple Windows PowerShell cmdlets to rename and reboot the computer or join the domain. &nbsp;Hey, Scripting Guy! It seems that I have been hand building a number of computers recently for a computer lab we are setting up at work. I have written a batch file that [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,46,16,168,47,3,45],"class_list":["post-11001","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-computer-accounts","tag-desktop-management","tag-domains","tag-general-management-tasks","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to replace netdom commands with simple Windows PowerShell cmdlets to rename and reboot the computer or join the domain. &nbsp;Hey, Scripting Guy! It seems that I have been hand building a number of computers recently for a computer lab we are setting up at work. I have written a batch file that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11001","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=11001"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11001\/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=11001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}