How to Join Computer to the AD domain using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To join any workgroup computer in the domain using PowerShell, we can use the Add-Computer command but before that, there are a few Windows prerequisite that DNS must be configured properly and the domain controller should be reachable and others should suffice then only PowerShell can use the command to join computer into a domain.

Add-Computer -ComputerName Test1-win2k16 `
             -DomainCredential Labdomain\Administrator `
             -DomainName Labdomain.local -Restart -Force -PassThru

Once you run the above command, it will ask you for the credential for the user you entered. In the above example, we are joining a remote computer to the domain LabDomain.Local and it will restart the remote system. If the users are logged in on the remote system, it might not restart but the system will connect to the domain. You can restart later forcefully.

We need to make sure that the username used in the -DomainCredential parameter, should have proper privileges like DomainAdmin, EnterpriseAdmin.

To Join the computer in the Different OU, you need to provide the OU path

Add-Computer -ComputerName Test1-win2k16 `
             -DomainCredential Labdomain\Administrator `
             -DomainName Labdomain.local `
             -OuPath 'OU=Prod,OU=Servers,DC=labdomain,DC=local' `
             -Restart -Force -PassThru

Here the computer Name parameter is String[] so you can use multiple computers.  For example,

Add-Computer -ComputerName Test1-win2k16, Test1-Win2k12, Test2-Win2k12 `
             -DomainCredential Labdomain\Administrator `
             -DomainName Labdomain.local `
             -OuPath 'OU=Prod,OU=Servers,DC=labdomain,DC=local' `
             -Restart -Force -PassThru

You can also use the foreach loop and the list of computers from the CSV file to join multiple computers if the OU path is different.

raja
Published on 28-Dec-2020 11:30:56
Advertisements