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.
PowerShell script to add a Windows device to the domain
1 2 3 4 5 6 7 8 |
$domain = " Your-domain-name " $username = " $domain\ domain-user " # User name with privileges to add a device to the $domain $password = " ******** " | ConvertTo-SecureString -AsPlainText -Force # Password for the above user $user2 = " Administrator " # Administrator account of the device $pass2 = " ******** " # Password for $user2 $lcred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($user2, ($pass2 | ConvertTo-SecureString -AsPlainText -Force)) $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($username, $password) Add-Computer -DomainName $domain -Credential $credential -LocalCredential $lcred -Restart -Force |
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.