PoSh script to rename network adapters and remove Register in DNS setting

I’ve had the need to automate the renaming the network adapters and edit DNS settings of numerous Hyper-V hosts (think dozens). PoSh to the rescue:

$banner = @"
=======================================
Rename Network Cards and change a DNS setting
Author (or rather modify-er!)- Manny Sidhu

- Running this script will rename all adapters.
- This script wont remove IP addressing.
- This script requires the niclist.csv to be in same folder C:\temp
- This script will remove the check box to register connections in DNS.
=======================================
"@
Write-host $banner -foregroundcolor "black" -backgroundcolor "yellow"
Write-Host "Press x key and enter to cancel OR any other key and enter to continue..."
$continue = Read-Host
If ($continue -eq "x")
{exit}
else
{
$coll = @()
$cname = $env:computername
$coll = import-csv C:\temp\niclist.csv | where {$_.host -eq $cname}
$adapters = $()

write-host "Checking and renaming NICs..."

"Preparing network adapters..."
$nics = get-netadapter -physical
Function get-word {(-join ((65..90) + (97..122) | Get-Random -Count 12 | % {[char]$_}))}
foreach ($nic in $nics){
$holder = get-word
rename-netadapter -name $nic.name -newname "$holder"}
$adapters = get-netadapter -physical
foreach ($interface in $adapters){
if($interface.MacAddress -eq $coll.mgt1)
{Rename-netadapter -name $interface.Name -NewName "$env:computername-MGT1"
Disable-NetAdapterBinding -Name "$env:computername-MGT1" -ComponentID ms_tcpip6}

elseif($interface.MacAddress -eq $coll.mgt2)
{Rename-netadapter -name $interface.Name -NewName "$env:computername-MGT2"
Disable-NetAdapterBinding -Name "$env:computername-MGT2" -ComponentID ms_tcpip6}

elseif($interface.MacAddress -eq $coll.bck1)
{Rename-netadapter -name $interface.Name -NewName "$env:computername-BCK1"
Disable-NetAdapterBinding -Name "$env:computername-BCK1" -ComponentID ms_tcpip6
Get-NetAdapter -Name "$env:computername-BCK1" | Get-NetIPConfiguration | Get-NetConnectionProfile | where Interfacealias -eq "$env:computername-BCK1" | Set-DnsClient -RegisterThisConnectionsAddress:$false -Verbose}

elseif($interface.MacAddress -eq $coll.bck2)
{Rename-netadapter -name $interface.Name -NewName "$env:computername-BCK2"
Disable-NetAdapterBinding -Name "$env:computername-BCK2" -ComponentID ms_tcpip6
Get-NetAdapter -Name "$env:computername-BCK2" | Get-NetIPConfiguration | Get-NetConnectionProfile | where Interfacealias -eq "$env:computername-BCK2" | Set-DnsClient -RegisterThisConnectionsAddress:$false -Verbose}

elseif($interface.MacAddress -eq $coll.VM1)
{Rename-netadapter -name $interface.Name -NewName "$env:computername-VM1"
Disable-NetAdapterBinding -Name "$env:computername-VM1" -ComponentID ms_tcpip6
Get-NetAdapter -Name "$env:computername-VM1" | Get-NetIPConfiguration | Get-NetConnectionProfile | where Interfacealias -eq "$env:computername-VM1" | Set-DnsClient -RegisterThisConnectionsAddress:$false -Verbose}

elseif($interface.MacAddress -eq $coll.VM2)
{Rename-netadapter -name $interface.Name -NewName "$env:computername-VM2"
Disable-NetAdapterBinding -Name "$env:computername-VM2" -ComponentID ms_tcpip6
Get-NetAdapter -Name "$env:computername-VM2" | Get-NetIPConfiguration | Get-NetConnectionProfile | where Interfacealias -eq "$env:computername-VM2" | Set-DnsClient -RegisterThisConnectionsAddress:$false -Verbose}}

"All Adapters are ready..."
get-netadapter -Physical | select name, Macaddress, interfacedescription | sort interfacedescription | ft -AutoSize

Write-host "All done successfully!"
}

Note – A similar script exists somewhere in wild out there, this one is a modified version of that.

Leave a Comment

Your email address will not be published.