Skip to content

Powershell script change IP, Gateway and DNS of NIC

I have recently setup a VM which I wanted to quickly put online/offline or switch between my router Gateway and server Gateway Address. I got tired of clicking through multiple windows and ok buttons and decided to write 2 powershell scripts to do this with one click.
The first script sets the interface Gateway and DNS to 192.168.0.1. The second script reverses this and sets the Gateway and DNS to 192.168.0.11

Please note I am using Powershell v5 on Windows 10 Technical Preview. I have not tested this on previous versions but I think it should work in v3 and v4. I know in v2 there was more work involved and Cmdlets which read WMI were required. In PS v5 this is easier to do but I had to use the Cmdlets such as Remove-NetIPAddress before Set-IPAddress.

Other Cmdlets I found useful to check the Addresses of the interface were:
Get-NetIPConfiguration
Get-NetIPConfiguration -InterfaceAlias ‘Ethernet0 2’ -Detailed
Get-DnsClientServerAddress -InterfaceAlias ‘Ethernet0 2’

Script 1 – SetGatewayDNSto0.1.ps1

#Powershell 5.0 script to change Gateway and DNS Address of specific NIC
#Imran Qureshi, Date: 08/03/2015 URL: https://imranqureshi.co.uk

#Removes the IP and Gateway Address Remove-NetIPAddress -InterfaceAlias 'Ethernet0 2' -DefaultGateway 192.168.0.11

 #Set the same IP Address and other Gateway Address New-NetIPAddress -InterfaceAlias 'Ethernet0 2' -IPAddress 192.168.0.22 -PrefixLength 24 -DefaultGateway 192.168.0.1

Set DNS Address Set-DnsClientServerAddress -InterfaceAlias 'Ethernet0 2' -ServerAddresses 192.168.0.1

Script 2 – SetGatewayDNSto0.11.ps1

#Powershell 5.0 script to change Gateway and DNS Address of specific NIC
# Imran Qureshi, Date: 08/03/2015 URL: https://imranqureshi.co.uk

#Removes the IP and Gateway Address
Remove-NetIPAddress -InterfaceAlias 'Ethernet0 2' -DefaultGateway 192.168.0.1

# Set the same IP Address and other Gateway Address
New-NetIPAddress -InterfaceAlias 'Ethernet0 2' -IPAddress 192.168.0.22 -PrefixLength 24 -DefaultGateway 192.168.0.11

#Set DNS Address
Set-DnsClientServerAddress -InterfaceAlias 'Ethernet0 2' -ServerAddresses 192.168.0.11
Share this Post
  •  
  •  
  •  
  •  
  •  
  •