Powershell

Automate joining a workstation to a domain using powershell and a batch file

If you are a network admin looking for an easy way to script the joining of a workstation to a domain perhaps out of an image restore, you can do this easily by using powershell as well as a batch file to call your powershell script.

Your Powershell Script

Open up your favorite text editor and enter the following code, replacing the variables below with the appropriate information specific to your domain:

$credential = New-Object System.Management.Automation.PsCredential(“%useraccount%”, (ConvertTo-SecureString “%password%” -AsPlainText -Force))YourOUhere,DC=YOURDC,DC=YOURDC”)

Add-Computer -DomainName “%userdomain%” -Credential $credential -OUPath (“OU=YourOUhere,OU=

Name the script above as a .PS1 file as you will call this file with your batch file.

Your Batch File

The batch file code will contain commands to allow the execution policy to run the PS1 file you are calling and then set your policy back to restricted.  *Note* – the “joinDomain.ps1” file will be the name of whatever you name your file you create above.
powershell Set-ExecutionPolicy Unrestricted

powershell c:\deploy\joinDomain.ps1

powershell Set-ExecutionPolicy Restricted

Subscribe to VirtualizationHowto via Email 🔔

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com and has over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, Brandon has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.