One-Liner: Query Network Connection With a Specific IP/DNS/… (Powershell)

This line will come in handy when you want to change the settings of a network connection based on its configurations not the connection name…

The below example will return the network connections that have a static IP configurations, and the DNS servers match 10.10.10.x

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=TRUE and DHCPEnabled=FALSE" | Where-Object { $_.DNSServerSearchOrder -match '^10\.10\.10\.' }

You can replace $_.DNSServerSearchOrder by the required value ( $_.IPAddress, $_.DefaultIPGateway,…..etc)

In on of the rel life examples, I wanted to make a script to modify the DNS settings of Network connections with a specific DNS servers value regardless the name of the connection, so I can run the script on multiple servers. The final script looked something like this:

$DNSServers = "20.20.20.1", "20.20.20.2"
$Networks = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=TRUE and DHCPEnabled=FALSE" | Where-Object { $_.DNSServerSearchOrder -match '^10\.10\.10\.' }
Foreach ($Network in $Networks){
$Network.SetDNSServerSearchOrder($DNSServers)
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s