VMware 2 min read
How to Change IP and Join a VM into Domain by PowerCLI in VMware
Michael Wu ·
There are several methods to change IP addresses and add VMs to a domain in VMware. Customization specifications can add the VM into domain after creation, however this approach may fail with complex DNS or network environments.
Tested Environments
- VMware vSphere PowerCLI 6.3 Release 1
- Windows Server 2012 R2
- PowerShell 5.0
- VMware vCenter 5.5
Change IP
After creating a VM with New-VM, modify the IP address using this script:
Add-PSSnapin VMware*
$hostname = "VMs Name"
$newIP = "10.10.10.10"
$newGateWay = $newIP.Split(".")[0] + "." + $newIP.Split(".")[1] + "." + $newIP.Split(".")[2] + ".1"
$cmdIP = "netsh interface ipv4 set address name=`"Ethernet 2`" static $newIP 255.255.255.0 $newGateWay"
$cmdDNS1 = "netsh interface ipv4 set dns name=`"Ethernet 2`" static 8.8.8.8"
$cmdDNS2 = "netsh interface ip add dns name=`"Ethernet 2`" 8.8.4.4 index=2"
$vm = Get-VM $hostname
$cred = Get-Credential Administrator
Invoke-VMScript -VM $vm -ScriptType Bat -ScriptText $cmdIP -Verbose -GuestCredential $cred
Invoke-VMScript -VM $vm -ScriptType Bat -ScriptText $cmdDNS1 -Verbose -GuestCredential $cred
Invoke-VMScript -VM $vm -ScriptType Bat -ScriptText $cmdDNS2 -Verbose -GuestCredential $cred
Join Domain (WMIC Method)
Domain joining requires both local administrator and domain account credentials:
Add-PSSnapin VMware*
$hostname = "VMs Name"
$vm = Get-VM $hostname
$cred = Get-Credential Administrator
$userID = whoami
$domain = "YourDomain.com"
$OU = "OU=yourOU;DC=yourDomain;DC=com"
$DomainAccountPWD = (Get-Credential $userID -Message "Please Enter your Domain account password.").GetNetworkCredential().Password
$cmd = "wmic.exe /interactive:off ComputerSystem Where name=`"%computername%`" call JoinDomainOrWorkgroup FJoinOptions=1 Name=`"$domain`" UserName=`"$userID`" Password=`"$pwd`" AccountOU=`"$OU`""
Invoke-VMScript -VM $vm -ScriptType Bat -ScriptText $cmd -Verbose -GuestCredential $cred
Update (04/04/2017): PowerShell Method
Add-PSSnapin VMware*
$hostname = "VMs Name"
$vm = Get-VM $hostname
$cred = Get-Credential Administrator
$userID = whoami
$DomainAccountPWD = (Get-Credential $userID -Message "Please Enter your Domain account password.").GetNetworkCredential().Password
$cmd = @"
`$domain = "YourDomain.com"
`$password = "$pwd" | ConvertTo-SecureString -asPlainText -force;
`$username = "$userID";
`$credential = New-Object System.Management.Automation.PSCredential(`$username, `$password);
Add-computer -DomainName `$domain -Credential `$credential
"@
Invoke-VMScript -VM $vm -ScriptText $cmd -Verbose -GuestCredential $cred
Two credential prompts appear: first for local admin access, second for domain credentials.