Powershell script on all enabled network adapters RRS feed

  • General discussion

  • Hello.  We've been running into some connectivity issues such as IP conflicts, etc., and it seems like we may have a few machines out there that have static IP addresses rather than getting them from our DHCP server.  I've got a PS script written to reconfigure any machines with static IPs to DHCP; however, it is currently set to only reconfigure the adapter with a status of "up."

    Unfortunately, we have quite a few laptops that switch back and forth between wireless and wired connections, so I would like the script to be able to reconfigure all Enabled NICs rather than just the one connected at that time.  Is there a simple edit to this that would work or do I need to take a wholly different approach?  Code is below.


    $IPType = "IPv4"
    $adapter = Get-NetAdapter | ? {$_.Status -eq "up"}
    $interface = $adapter | Get-NetIPInterface -AddressFamily $IPType
    $suffixes = ''
    If ($interface.Dhcp -eq "Disabled") {
     # Remove existing gateway
     If (($interface | Get-NetIPConfiguration).Ipv4DefaultGateway) {
     $interface | Remove-NetRoute -Confirm:$false
     # Enable DHCP
     $interface | Set-NetIPInterface -DHCP Enabled
     # Configure the DNS Servers automatically
     $interface | Set-DnsClientServerAddress -ResetServerAddresses
     # Set DNS Suffix Search Order
     Invoke-WmiMethod -Class win32_networkadapterconfiguration -Name setDNSSuffixSearchOrder -ArgumentList @($suffixes),$null
     # Set Connection Specific DNS Suffix and enable DDNS
     $interface | Set-DnsClient -ConnectionSpecificSuffix "" -RegisterThisConnectionsAddress $true -UseSuffixWhenRegistering $true

    • Changed type Bill_Stewart Friday, July 7, 2017 4:23 PM
    • Moved by Bill_Stewart Friday, July 7, 2017 4:23 PM This is not "fix/debug/rewrite my script for me" forum
    Wednesday, May 3, 2017 4:31 PM

All replies