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 (the IP addresses of the Active Directory domain controllers configured in the script). 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.

    The DNS Server for the AD domain is configured based on the domain controller IP addresses provided. This output showing success status is shown in the Show Output window.

  2. Pre-checking AD domain resolution

    Before attempting to add a device to the AD domain, the above script checks the resolution of the domain name and attempts the discovery of reachable domain controllers. It then proceeds to check connectivity to key ports such as LDAP (389), Kerberos (88) and SMB (445), if a domain controller is identified.

  3. The AD domain is checked for successful DNS resolution to the domain controllers, and further, the key ports of the domain controllers are checked for connectivity.

  4. Adding the device to the AD domain

    Administrators can add the device to the Active Directory (AD) domain once the DNS is configured and validated for resolution.In the script, 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.
    The operation to add a computer to the AD domain via PowerShell is finalized after the device restarts, as confirmed by the success status in the Show Output window.

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