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