Tuesday, May 8, 2012

Configuring the IP address on Windows 8 and Windows Server 2012 by PowerShell

On Windows 8 and Windows Server 2012, Microsoft introduces some cmdlets for configuring the IP address because Microsoft might remove the Netsh functionality for TCP/IP in further Windows versions.

Goal
Configuring the IPv4 address by PowerShell cmdlet on a Windows Server 2012.

1. On a server, log in as Administrator.
2. Launch "PowerShell".
3. Perform "Get-NetIPInterface" to get the network interface name.

 
The default name of the first network interface is "Ethernet" but there are two "Ethernet" in the above image. I need to select the "Ethernet" which is IPv4 of "AddressFamily". To update the IP address, I need to know the "Interface Alias" or "Interface Index" of the Network interface. The "Interface Index" of "Ethernet" is "12".

4. Perform "New-NetIPAddress -AddressFamily IPv4 -IPAddress 192.168.1.100 -PrefixLength 24 -DefaultGateway 192.168.1.1 -InterfaceIndex 12" to configure the IP address on "Ethernet".


5. Perform "Set-DnsClientServerAddress -ServerAddresses 8.8.8.8, 8.8.4.4 -InterfaceIndex 12 -PassThru" to configure the Preferred DNS server and Alternate DNS server.


6. Now, I would like to remove the DNS settings of "Ethernet". Perform "Set-DnsClientServerAddress -ResetServerAddresses -InterfaceIndex 12 -PassThru".

 
7. Perform "Remove-NetIPAddress -AddressFamily IPv4 -InterfaceIndex 12 -PassThru -Confirm:$false" to remove the IP address and subnet mask on "Ethernet".


The IP address and subnet mask was removed but the default gateway hasn't be removed by "Remove-NetIPAddress".

8. Perform "ipconfig /all" and "Get-NetRoute -AddressFamily IPv4 -InterfaceIndex 12" to verify it.


9. Perform "Remove-NetRoute -InterfaceIndex 12 -NextHop 192.168.1.1 -PassThru -Confirm:$false" to remove the default gateway.


As a result, the default gateway was removed on "Ethernet".

References:
New-NetIPAddress
http://technet.microsoft.com/en-us/library/hh826150.aspx

Remove-NetIPAddress
http://technet.microsoft.com/en-us/library/hh826145.aspx

Get-NetRoute
http://technet.microsoft.com/en-us/library/hh826126.aspx

Remove-NetRoute
http://technet.microsoft.com/en-us/library/hh826124.aspx

Get-NetIPInterface
http://technet.microsoft.com/en-us/library/hh826135.aspx

This posting is provided "AS IS" with no warranties, and confers no rights!

1 comment:

  1. Thank you, you really helped me with this.

    ReplyDelete