Step #1: This is the simplest method to add a computer to a domain. In this example you will be prompted for credentials followed by the required reboot.
1
2
|
Add-Computer -DomainName “your.domain.here”
Restart-Computer
|
Step #2: If you require an automated script without prompting the user for credentials you can provide the user account with rights to add computers to the domain.
1
2
3
|
$cred = New-Object System.Management.Automation.PsCredential(“domain\useraccountwithjoinpermissions”, (ConvertTo-SecureString “useraccountpassword” -AsPlainText -Force))
Add-Computer -DomainName “your.domain.here” -Credential $cred
Restart-Computer
|
Step #3: The following example shows how to specify the Organizational Unit in Active Directory where the computer account will reside.
1
2
3
|
$cred = New-Object System.Management.Automation.PsCredential(“domain\useraccountwithjoinpermissions”, (ConvertTo-SecureString “useraccountpassword” -AsPlainText -Force))
Add-Computer -DomainName “your.domain.here” -Credential $cred -OUPath “OU=computers,OU=yourlocation,DC=your,DC=domain,DC=here”
Restart-Computer
|
Step #4: The following example highlights how you can add a computer to the domain as a new computer name without a reboot in between. Imaging processes may initially assign your computer a random name that requires changing to include a specific naming standard.
1
2
3
4
|
$cred = New-Object System.Management.Automation.PsCredential(“domain\useraccountwithjoinpermissions”, (ConvertTo-SecureString “useraccountpassword” -AsPlainText -Force))
Add-Computer -DomainName “your.domain.here” -Credential $cred -OUPath “OU=Computers,OU=YourLocation,DC=your,DC=domain,DC=here”
Rename-Computer -NewName $newcomputername -DomainCredential $credential -Force
Restart-Computer
|
Step #5: The final example allows for input from the user to determine both the computer name and the Active Directory location. This utilizes Write-Host and Read-Host with a small amount of IF THEN validation to accomplish this. If someone does not enter any value it will use the Default value as displayed to the user. For the numbers 1-4 option if an invalid character is entered it will also use the specified Default setting.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
Write-Host “Please enter your desired computer name: [Default $env:computername]:”
$computername = Read-Host
$renamecomputer = $true
if ($computername -eq “” -or $computername -eq $env:computername) { $computername = $env:computername; $renamecomputer = $false }
Write-Host “Please enter your desired location [1-4] [Default 1]:
1. Chicago
2. Paris
3. Sydney
4. Toronto”
$ou = Read-Host
$validate = $false
if ($ou -eq “” -or $ou -eq “1”) { $ou = “OU=Computers,OU=Chicago,DC=your,DC=domain,DC=here”; $validate = $true }
if ($ou -eq “2”) { $ou = “OU=Computers,OU=Paris,DC=your,DC=domain,DC=here”; $validate = $true }
if ($ou -eq “3”) { $ou = “OU=Computers,OU=Sydney,DC=your,DC=domain,DC=here”; $validate = $true }
if ($ou -eq “4”) { $ou = “OU=Computers,OU=Toronto,DC=your,DC=domain,DC=here”; $validate = $true }
if ($validate -eq $false) { Write-Host “Invalid input, defaulting to [1].”; $ou = “OU=Computers,OU=Chicago,DC=your,DC=domain,DC=here”}
$credentials = New-Object System.Management.Automation.PsCredential(“yourdomain\useraccountwithjoinpermissions”, (ConvertTo-SecureString “useraccountpassword” -AsPlainText -Force))
Write-Host “Adding $computername to the domain”
Add-Computer -DomainName “your.domain.here” -Credential $credentials -OUPath $ou
if ($renamecomputer -eq $true) { Rename-Computer -NewName $computername -DomainCredential $credentials -Force }
Restart-Computer
|
Step #6: The following is a Function example that can be used within a script or from a GUI compliments from a reader.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
function Join-Domain {
Param(
[Parameter(Position=0)]
[String]$computername = $env:computername,
[Parameter(Position=1)]
[ValidateSet(“OU=Computers,OU=Chicago,DC=your,DC=domain,DC=here”,”OU=Computers,OU=Paris,DC=your,DC=domain,DC=here”,”OU=Computers,OU=Sydney,DC=your,DC=domain,DC=here”,”OU=Computers,OU=Toronto,DC=your,DC=domain,DC=here”)]
[string]$OU = “CN=Computers,OU=Chicago,DC=your,DC=domain,DC=here”
)
$renamecomputer = $true
if ($computername -eq “” -or $computername -eq $env:computername) { $computername = $env:computername; $renamecomputer = $false }
$credentials = New-Object System.Management.Automation.PsCredential(“yourdomain\useraccountwithjoinpermissions”, (ConvertTo-SecureString “useraccountpassword” -AsPlainText -Force))
Write-Verbose “Adding $computername to the domain under $OU”
Add-Computer -DomainName “your.domain.here” -Credential $credentials -OUPath $OU
if ($renamecomputer -eq $true) { Rename-Computer -NewName $computername -DomainCredential $credentials -Force }
}
|
Enjoy!
Got a project that needs expert IT support?
From Linux and Microsoft Server to VMware, networking, and more, our team at CR Tech is here to help.
Get personalized support today and ensure your systems are running at peak performance or make sure that your project turns out to be a successful one!
CONTACT US NOW