Category filter

Script to add Windows devices to Active Directory domain

Windows devices can be added to an organization’s Active Directory (AD) domain, so that users can log onto their devices using their AD credentials. Adding devices to the domain can be accomplished by utilizing PowerShell scripts. The scripts provided in this article can confirm successful domain DNS server configuration and resolution, essential for successful domain joining of device. These scripts can be executed from the Hexnode portal using the Execute Custom Script remote action.

Supported Versions:


The script is supported on the following Windows versions:

  • Windows 10 v1803+
  • Windows 10 v1703 to Windows 10 v1709 (if .NET Framework v4.7.1+ is installed on the device)
  • Windows 11 (Pro, Enterprise, Education)
Disclaimer:


The sample scripts provided below are adapted from third-party open-source sites.

PowerShell scripts

Execute the following scripts in sequence for adding the Windows device to the AD domain successfully.

  1. Configuring the DNS Server

    This script configures the target devices to use specific DNS servers that are the IP addresses of the Active Directory domain controllers. All active network adaptors are updated with the provided DNS addresses (Domain controllers). Further, a resolution test is conducted against YourDomain.local to confirm domain resolution.

  2. Pre-checking AD domain resolution

    Before attempting to add a device to the AD domain, the AD domain resolution can be checked using the above script to ensure successful device joining. The script checks whether the domain name can be resolved, attempts to discover a reachable domain controller, and then proceeds to check connectivity to key ports such as LDAP (389), Kerberos (88) and SMB (445), if a domain controller is identified.

  3. Adding the device to the AD domain

    The device can be added to the AD domain, whose DNS was configured and validated for resolution. Configure the domain name ($domain), user with administrator privileges to join a device to the domain ($user), the user password ($password) and the OU ($ou – if required). The Add-Computer cmdlet is used to facilitate the joining. The device is forcefully restarted to complete domain join.

What happens at the device end?

Upon successful execution of the script, the device will undergo an automatic restart. Afterwards, the device is joined to the Active Directory (AD) domain, allowing users to log in to the device using their AD credentials. Upon login, checking Settings > Accounts > Access work or school confirms the connection to the designated AD domain.

Add Windows computers to AD domain with PowerShell and ensure connectivity from the device settings

Notes:

  • You can also execute the Join AD Domain action to add your Windows devices AD domain remotely at your convenience.
  • Windows 10 Home editions do not support domain-join. Hence, make sure the devices permit domain-joining before executing the script.
  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

Sample Script Repository