DevOps | Scripts | Automation

CMDPowershell

How to join Windows Server to domain?

To join a Machine to the AD domain, we have various options (GUI + Scripting). In this article, we see those options.

  • From Computer Properties (GUI)
  • From Command Line
  • From PowerShell.
  • From Azure PowerShell
Pre-Requisites:

There are some prerequisites before you join the machine to a domain. Some basic requirements are mentioned, but not a detailed ones. You can google for the detailed requirements to join VMs in the domain.

Below are the basic requirements.

  • Both Servers (AD and VM to be joined) should have proper connectivity and be reachable to each other.
  • VM DNS address should be set properly.
  • VM Windows Firewall should have an LDAP port open.
  • Domain Joining username and password (Domain Admin minimum).

Let’s check how we can join a computer to domain.

From Computer Properties

Right Click on the Computer -> Properties. You will see the below window.

Computer Properties

Click on Change Settings and click on Change.

Change Settings

Enter the domain name and click OK. There you will be prompted for UserName and Password.

Add Domain Name
Enter Domain credentials

You will see the domain joining welcome window.

Domain Join window

You must restart the server after it takes effect. If you have an access to AD then you can see the Joined computer in the Default Computers OU.

If we need to join multiple VMs to the domain then this process will be tedious. We need to use the scripting for it that is explained below.

Using PowerShell

To add the computer to a domain, you need to use the Add-Computer command in PowerShell as shown below.

$creds = Get-Credential -Message "Enter Domain Admin Credentials"
Add-Computer -ComputerName Server2 -DomainName Labdomain.local -Credential $creds -Restart -Force

The above command will add the Server2 to domain name LabDomain.local. You can run this command remotely. If you are running this command from the local computer then remove –ComputerName parameter.

If you have multiple computers to Join then you can use the ComputerName parameter as -ComputerName Server1, Server2, Server3.

Refer to the below URL to know more about this command.

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/add-computer?view=powershell-5.1

Using Cmd

To join the VM to a domain using cmd, need to run netdom command on the computer that we need to join. The below command should be run from the local computer.

netdom join %computername% /domain:DomainName.test /UserD:DomainAdminLevelUser /PasswordD:*

The above command will prompt you for the password and once you type it will be invisible.

Output of cmd domain joining

Once domain is joined, you will see the output as below. You need to restart system after that.

Output of System joined in domain

Instead of * you can also provide the password directly but that will be in the plain text and its not recommended.

If you are running this command from AD server or from the server in the same network then provide Server name instead of %ComputerName%. The below command also works remotely.

netdom join Server2 /domain:DomainName.test /UserD:DomainAdminLevelUser /PasswordD:*
Using Azure PowerShell

You can also join the Azure VMs which are in workgroup using Azure VM extension directly without login to Windows VM. If you have the bulk VMs then this method will be quite easier.

I have already created a script to join VM to domain. You can check the code in the below github repository.

https://github.com/chiragce17/MyPublicRepo/tree/main/Azure/Join-VMtoDomain

One thought on “How to join Windows Server to domain?

  • Interesting post, thanks for sharing.

Comments are closed.