Tested with Windows 7
Type: PowerShell 3.0 << Powershell 3.0 found here:
http://www.microsoft.com/en-us/download/details.aspx?id=34595
# Script Usage:
# <ScriptName.ps1 MyNewComputerName>
param
([Parameter(Position=0,mandatory=$true)]
[string]$newname) # Required Parameter, script will fail without it
# === Set the Variables ===
$domain = "Domain" # NETBIOS name of your domain
$DNSDomain = 'domain.local' # FQDN of your domain
$domainUserroot = 'ADUser' # An AD user with permissions to join workstations to the domain
$domainpass = 'supersecret' # The password for the above user$domainUser = $domain+'\'+$domainUserroot $securePass = ConvertTo-SecureString –String $domainpass -AsPlainText –force $domaincred = New-Object System.Management.Automation.PSCredential` $domainUser,$securePass
$OU = "OU=Computers-Win7,DC=domain,DC=local" # Target OU
# === Start of Commands ===# Add the computer to the domain
Add-Computer -DomainName $dnsdomain -Credential $domaincred -OUPath $OU
# Rename the Computer - Requires Domain Credentials
Rename-Computer -NewName $newname -DomainCredential $domaincred # Rename Computer
Restart-Computer # Reboot the Computer
A note about the password: Due to the nature of PSH, passing credentials is rather difficult, especially if you want to encrypt them. I found it easier to create an AD user and lock that user account down so that it is allowed to join workstations to the specific container, rather than the former. # <ScriptName.ps1 MyNewComputerName>
param
([Parameter(Position=0,mandatory=$true)]
[string]$newname) # Required Parameter, script will fail without it
# === Set the Variables ===
$domain = "Domain" # NETBIOS name of your domain
$DNSDomain = 'domain.local' # FQDN of your domain
$domainUserroot = 'ADUser' # An AD user with permissions to join workstations to the domain
$domainpass = 'supersecret' # The password for the above user$domainUser = $domain+'\'+$domainUserroot $securePass = ConvertTo-SecureString –String $domainpass -AsPlainText –force $domaincred = New-Object System.Management.Automation.PSCredential` $domainUser,$securePass
$OU = "OU=Computers-Win7,DC=domain,DC=local" # Target OU
# === Start of Commands ===# Add the computer to the domain
Add-Computer -DomainName $dnsdomain -Credential $domaincred -OUPath $OU
# Rename the Computer - Requires Domain Credentials
Rename-Computer -NewName $newname -DomainCredential $domaincred # Rename Computer
Restart-Computer # Reboot the Computer