How to Add a Computer to a Domain Using PowerShell

In today’s tutorial, we will discuss an advanced topic. Here, I will explain how to add a computer to a domain using PowerShell. This will help you to add computers to your domain, ensuring they are correctly configured and managed within your network.

Prerequisites

Here are some prerequisites that you should follow before trying to add a computer to a domain:

  1. Administrative privileges on the computer you want to add to the domain.
  2. The domain name and credentials of an account with permission to join computers to the domain.
  3. PowerShell is installed on your system (most Windows systems come with PowerShell pre-installed).

Add a Computer to a Domain Using PowerShell

Follow the steps below to add a computer to a domain using PowerShell.

Step 1: Open PowerShell as an Administrator

To perform administrative tasks, you need to run PowerShell with elevated privileges. Right-click the PowerShell icon and select “Run as Administrator.”

Step 2: Check Network Connectivity

Ensure that the computer can communicate with the domain controller. Use the Test-Connection cmdlet to ping the domain controller:

Test-Connection -ComputerName "DC01.usa.example.com"

Replace DC01.usa.example.com with the actual domain controller name. If the ping is successful, you can proceed to the next step.

Step 3: Add the Computer to the Domain

Use the Add-Computer cmdlet to join the computer to the domain. Here is an example command:

Add-Computer -DomainName "usa.example.com" -Credential (Get-Credential) -Restart
  • -DomainName: Specifies the domain to join.
  • -Credential: Prompts for the username and password of an account with domain join permissions.
  • -Restart: Reboots the computer after joining the domain.

When you run this command, a prompt will appear asking for the credentials. Enter the username and password of an account that has permission to add computers to the domain.

Step 4: Verify the Computer is Added to the Domain

After the computer restarts, log in with a domain account to verify that the computer is now part of the domain. You can also use the Get-ComputerInfo cmdlet to check the domain status:

Get-ComputerInfo | Select-Object CsName, CsDomain

This is how to add a computer to a domain using PowerShell.

Check out Set the Default Printer Using PowerShell in Windows

Add a Computer to a Specific Organizational Unit (OU)

In some cases, you may want to add a computer to a specific OU within the domain. Use the -OUPath parameter with the Add-Computer cmdlet:

Add-Computer -DomainName "usa.example.com" -OUPath "OU=Computers,OU=USA,DC=usa,DC=example,DC=com" -Credential (Get-Credential) -Restart

Join Multiple Computers to the Domain

For large-scale deployments, you can use a script to join multiple computers to the domain. Here’s an example script:

$computers = @("Comp1", "Comp2", "Comp3")
foreach ($computer in $computers) {
    Invoke-Command -ComputerName $computer -ScriptBlock {
        Add-Computer -DomainName "usa.example.com" -Credential (Get-Credential) -Restart
    }
}

This script loops through a list of computer names and uses Invoke-Command to run the Add-Computer cmdlet on each one.

Check out Set Password Never Expires for Local User Using PowerShell

Troubleshooting Common Issues

I am sure you will face some errors while trying to add a computer to a domain. I am sharing a few common mistakes that you may encounter and their corresponding fixes.

Issue 1: Incorrect Domain Credentials

If you receive an error indicating that the credentials are incorrect, double-check the username and password. Ensure that the account has the necessary permissions to join computers to the domain.

Issue 2: Network Connectivity Problems

If the computer cannot communicate with the domain controller, verify the network settings. Ensure that the computer is connected to the correct network and that there are no firewall rules blocking communication.

Issue 3: DNS Resolution Issues

Ensure that the computer can resolve the domain name. You can test this by using the nslookup command:

nslookup usa.example.com

If the domain name cannot be resolved, check the DNS settings on the computer and ensure it points to a DNS server that can resolve the domain.

Conclusion

In this tutorial, I have explained how to add a computer to a domain using PowerShell. Also, I have provided a few common issues and fixes. Do let me know in the comments below if you still have any issues.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.