Here are the basic steps how PowerShell can join a computer to an AD domain:
# do not store passwords in production solutions,
# or you MUST control access permissions to this sensitive data
$username = "mydomain\UserName"
$password = 'Password' | ConvertTo-SecureString -AsPlainText -Force
$domainName = 'NameOfDomain'
# convert username and password to a credential
$cred = [PSCredential]::new($username, $password)
# join computer
Add-Computer -DomainName $domainName -Credential $cred
# restart computer
You may want to adjust this code to your needs. The example stores the password in plain text which is not safe. You may want to load it from a file.
ReTweet this Tip!