POWERSHELL: UPDATE STATIC DNS SERVER IP ADDRESSES

This script will pull a list of servers from Active Directory and automatically update their static DNS IP addresses.

It has logic in it to exclude servers with names containing “-DC”, useful for domain controllers. You do NOT want to change domain controller DNS server IP’s as they are setup a little differently to work properly in Active Directory.

$Computers = Get-ADComputer -Filter '(OperatingSystem -like "Windows Server*") -and (Name -NotLike "*-DC*")' | Sort-Object Name
$NewDnsServerSearchOrder = "10.0.0.11","10.0.0.12"

ForEach ($Computer in $Computers) {
	Write-Host "$($Computer.Name): " -ForegroundColor Yellow
	Invoke-Command -ComputerName $Computer.Name -ScriptBlock {
		$Adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -ne $True -and $_.DNSServerSearchOrder -ne $null}
		
		# Show DNS servers before update
		Write-Host "Before: " -ForegroundColor Green
		$Adapters | ForEach-Object {$_.DNSServerSearchOrder}

		# Update DNS servers
		$Adapters | ForEach-Object {$_.SetDNSServerSearchOrder($NewDnsServerSearchOrder)} | Out-Null

		# Show DNS servers after update
		$Adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -ne $True -and $_.DNSServerSearchOrder -ne $null}
		Write-Host "After: " -ForegroundColor Green
		$Adapters | ForEach-Object {$_.DNSServerSearchOrder}
	}
}