Category filter

Script to add Windows devices to Active Directory domain

Adding Windows devices to an Active Directory domain is no more a hassle. You can get it done quickly by creating PowerShell scripts to be executed from the Hexnode portal. It adds the device to the specified domain without requiring direct access to the device.

Disclaimer:

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

PowerShell script to add a Windows device to the domain

This PowerShell script automates the process of adding a computer to a specified domain. It begins by defining the domain, domain user credentials, and local administrator account details. Secure credentials are then created for both a local administrator account ($lcred) and a domain user account ($credential) using the ConvertTo-SecureString cmdlet. The script utilizes the Add-Computer cmdlet to automatically join the Windows device to a specified domain, employing both domain user ($credential) and local administrator ($lcred) credentials. Following this, an automatic restart is initiated, and the operation is executed without user confirmation, using the -Force parameter.

  • The Add-Computer command generally takes the following syntax:

    Add-Computer

    [-ComputerName <String[]>]

    [-LocalCredential <PSCredential>]

    [-UnjoinDomainCredential <PSCredential>]

    [-Credential <PSCredential>]

    [-DomainName] <String>

    [-OUPath <String>] [-Server <String>]

    [-Unsecure]

    [-Options <JoinOptions>]

    [-Restart]

    [-PassThru]

    [-NewName <String>]

    [-Force]

    [-WhatIf]

    [-Confirm]

    [<CommonParameters>]

    Depending on your need, you may alter the attributes. For instance,

    Add-Computer -DomainName domainname –OUPath "OU=HexnodeOU CN=Users, DC=Hexnode,DC=local"

    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