Every time I set up a new VM in my lab, I usually set static IP configuration and join it to the domain. Mostly, MDT does that for me but sometimes I need to do it “manually”…

But in real-life, this is how to set IP configuration and join server to domain using PowerShell:

$ipaddress = “192.168.1.50”
$ipif = “LAN”
$ipprefix = “24”
$ipgw = “192.168.1.1”
$ipdns = “192.168.1.10,192.168.1.11”
$domainname = “domain.local”
$credential= “DOMAIN\username”
Rename-NetAdapter -Name “Ethernet” -NewName $ipif
Set-DnsClientServerAddress $ipif -ServerAddresses $ipdns
New-NetIPAddress -InterfaceAlias $ipif -IPAddress $ipaddress -PrefixLength $ipprefix -DefaultGateway $ipgw
Add-Computer -DomainName $domainname –Credential $credential
Restart-Computer -force

Some options. If you want it to make it more flexible and want the script to ask you for an IP-address every time, put the following instead:

$ipaddress = Read-Host “Enter IP-address”

If you really want to hardcode the password in the script (hey, it’s a lab so I guess nobody would care). Replace the above $credential = “DOMAIN\username” with:

$username = “DOMAIN\username”
$password = ConvertTo-SecureString -String “P@sSwOrd” -AsPlainText -Force
$credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $username, $password

Any other PowerShell hacks you do all the time in your lab?