Tuesday, May 23, 2017

PowerShell - Find VPN adapter and change DNS addresses

If we have to join VPN, the DNS will mostly be set during the joining time. If we want to change the DNS servers after connecting to VPN, we have to manually do it every time. It involves so many clicks to change. 
Below is PowerShell script which finds a Cisco VPN adapter and change its DNS address

$index = Get-NetAdapter | where InterfaceDescription -like 'Cisco*' | Select ifIndex

Set-DnsClientServerAddress -InterfaceIndex $index.ifIndex -ServerAddresses "1.1.1.1,1.1.1.2"

This script has to run in admin mode to get it applied. Better save the above 2 lines as .ps1 file and execute in admin mode.

The first line can be further optimized to check whether VPN connected or not, exact name of adapter etc...

The PowerShell module seems already installed in Win 10 machines. The API may differ for other versions.

No comments: