PowerShell join computer to a domain

To join a Windows computer to a domain using PowerShell use the following script. You can read my previous post, here on how to create the password file.

#set domain, credential, and hostname variables
$File = "C:\Scripts\Password.txt"
$Key = (1..32)
$UserName = "contoso.com\adjoin"
$Password = Get-Content $File | ConvertTo-SecureString -Key $Key
$DomainJoinCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName,$Password
$Domain = "contoso.com"
$OU = "OU=ContosoComputers,DC=contoso,DC=com"

#Join computer to domain
Add-Computer -DomainName $Domain -Credential $DomainJoinCredentials -OUPath $OU

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.