Joining a Computer to a Domain and Removing a Computer from a Domain

Joining a Computer to a Domain (Locally)

To join a PC to an Active Directory domain, run the following PowerShell script locally on the computer:

$dc = "ENTERPRISE" # Specify the domain to join
$pw = "Password123" | ConvertTo-SecureString -asPlainText –Force # Specify the password for the domain admin
$usr = "$dc\T.Simpson" # Specify the domain admin account
$creds = New-Object System.Management.Automation.PSCredential($usr,$pw)
Add-Computer -DomainName $dc -Credential $creds -restart -force -verbose
# Note that the computer will be restarted automatically

The computer will restart and then join the domain; it will be added to the default container.

Joining a Computer Remotely

To join a computer to a DC remotely, enhance the script this way:

$dc = "ENTERPRISE"
$pw = "Password123" | ConvertTo-SecureString -asPlainText -Force
$usr = "$dc\T.Simpson"
$pc = "R07GF" # Specify the computer that should be joined to the domain
$creds = New-Object System.Management.Automation.PSCredential($usr,$pw)
Add-Computer -ComputerName $pc -LocalCredential $pc\admin `
  -DomainName $dc -Credential $creds -Verbose -Restart -Force

Important Notes:

  • The $pc variable and –LocalCredential parameter are used to authenticate the computer to the domain
  • You must disable the firewall on the local computer for this method to work

Joining Multiple Computers

You can add more than one computer to the domain by either specifying them in the command line as a comma-delimited list or importing their names from a text file.

Comma-Delimited List

$dc = "ENTERPRISE"
$pw = "Password123" | ConvertTo-SecureString -asPlainText -Force
$usr = "$dc\T.Simpson"
$pc = "WKS034, WKS052, WKS057" # Specify the computers to join
$creds = New-Object System.Management.Automation.PSCredential($usr,$pw)
Add-Computer -ComputerName $pc -LocalCredential $pc\admin `
  -DomainName $dc -Credential $creds -Restart -Force

From Text File

$dc = "ENTERPRISE"
$pw = "Password123" | ConvertTo-SecureString -asPlainText -Force
$usr = "$dc\T.Simpson"
$pc = Get-Content -Path C:\Computers.txt # Specify the path to the computers list
$creds = New-Object System.Management.Automation.PSCredential($usr,$pw)
Add-Computer -ComputerName $pc -LocalCredential $pc\admin `
  -DomainName $dc -Credential $creds -Restart -Force

Removing a Computer from a Domain

To remove a computer from a domain remotely, use the Remove-Computer cmdlet. Since we’re removing from a domain, no local credentials are needed:

$dc = "ENTERPRISE"
$pw = "Password123" | ConvertTo-SecureString -asPlainText -Force
$usr = "$dc\T.Simpson"
$pc = "R07GF"
$creds = New-Object System.Management.Automation.PSCredential($usr,$pw)
Remove-Computer -ComputerName $pc -Credential $creds –Verbose –Restart –Force

Removing Multiple Computers

To remove multiple computers using a list in a TXT file, use the script above for joining computers to a DC, replacing the Add-Computer cmdlet with Remove-Computer. You will still need domain admin credentials to complete this unjoin operation.