Basic Network Configuration with Windows 8
With Windows Server “8”, you no longer have to use netsh
to set IP
addresses from the command-line. Thanks to the new NetTcpip module,
you can do a whole lot more network configuration via PowerShell.
Here is a listing of cmdlets included in the NetTcpip module:
PS C:\> Get-Command -Module NetTcpip
Capability Name ModuleName
---------- ---- ----------
CIM Get-NetIPAddress NetTcpip
CIM Get-NetIPInterface NetTcpip
CIM Get-NetIPv4Protocol NetTcpip
CIM Get-NetIPv6Protocol NetTcpip
CIM Get-NetNeighbor NetTcpip
CIM Get-NetOffloadGlobalSetting NetTcpip
CIM Get-NetPrefixPolicy NetTcpip
CIM Get-NetRoute NetTcpip
CIM Get-NetTCPConnection NetTcpip
CIM Get-NetTCPSetting NetTcpip
CIM Get-NetTransportFilter NetTcpip
CIM Get-NetUDPEndpoint NetTcpip
CIM Get-NetUDPSetting NetTcpip
CIM New-NetIPAddress NetTcpip
CIM New-NetNeighbor NetTcpip
CIM New-NetPrefixPolicy NetTcpip
CIM New-NetRoute NetTcpip
CIM New-NetTransportFilter NetTcpip
CIM Remove-NetIPAddress NetTcpip
CIM Remove-NetNeighbor NetTcpip
CIM Remove-NetPrefixPolicy NetTcpip
CIM Remove-NetRoute NetTcpip
CIM Remove-NetTransportFilter NetTcpip
CIM Set-NetIPAddress NetTcpip
CIM Set-NetIPInterface NetTcpip
CIM Set-NetIPv4Protocol NetTcpip
CIM Set-NetIPv6Protocol NetTcpip
CIM Set-NetNeighbor NetTcpip
CIM Set-NetOffloadGlobalSetting NetTcpip
CIM Set-NetPrefixPolicy NetTcpip
CIM Set-NetRoute NetTcpip
CIM Set-NetTCPSetting NetTcpip
CIM Set-NetUDPSetting NetTcpip
To view the IP addresses on a given interface, use Get-NetIpAddress:
PS C:\> Get-NetIPAddress -InterfaceIndex 17 | ft -AutoSize
ifIndex IPv4Address IPv6Address PrefixLength SuffixOrigin Store
------- ----------- ----------- ------------ ------------ -----
17 fe80::901:6a7e:d676:2a88%17 64 Link Active
17 2001:470:8:1ee:901:6a7e:d676:2a88 64 Link Active
17 169.254.42.136 16 Link Active
As you can see, I only have an auto-configured IPv4 address on this particular interface. You can remove addresses with Remove-NetIPAddress, like so:
PS C:\> Get-NetIPAddress -InterfaceIndex 17 -AddressFamily IPv4 | Remove-NetIPAddress
Confirm
Are you sure you want to perform this action?
Performing operation "Remove" on Target "NetIPAddress -IPv4Address 192.168.2.3 -InterfaceIndex 17 -Store Active"
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y
To view route information, use Get-NetRoute:
PS C:\> Get-NetRoute -InterfaceIndex 17
ifIndex RouteMetric DestinationPrefix NextHop Store
------- ----------- ----------------- ------- -----
17 1 0.0.0.0/0 192.168.2.1 Persistent
17 256 255.255.255.255/32 0.0.0.0 Active
17 256 224.0.0.0/4 0.0.0.0 Active
17 256 192.168.2.3/32 0.0.0.0 Active
17 1 0.0.0.0/0 192.168.2.1 Active
17 256 ff00::/8 :: Active
17 256 fe80::901:6a7e:d676:2a88/128 :: Active
17 256 fe80::/64 :: Active
17 256 2001:470:8:1ee:901:6a7e:d676:2a88/128 :: Active
17 256 2001:470:8:1ee::/64 :: Active
17 256 ::/0 fe80::20d:b9ff:fe15:e69d Active
PS C:\> Get-NetRoute -InterfaceIndex 17 -RouteMetric 1
ifIndex RouteMetric DestinationPrefix NextHop Store
------- ----------- ----------------- ------- -----
17 1 0.0.0.0/0 192.168.2.1 Persistent
17 1 0.0.0.0/0 192.168.2.1 Active
…and to remove routes, use the appropriately-named Remove-NetRoute cmdlet:
PS C:\> Get-NetRoute -InterfaceIndex 17 -RouteMetric 1 | Remove-NetRoute
Confirm
Are you sure you want to perform this action?
Performing operation "Remove" on Target "NetRoute -DestinationPrefix 0.0.0.0/0 -InterfaceIndex 17 -NextHop 192.168.2.1
-Store Persistent"
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y
Confirm
Are you sure you want to perform this action?
Performing operation "Remove" on Target "NetRoute -DestinationPrefix 0.0.0.0/0 -InterfaceIndex 17 -NextHop 192.168.2.1
-Store Active"
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y
Finally, to assign a static IP address to an interface, use the New-NetIpAddress cmdlet.
PS C:\> New-NetIPAddress -InterfaceIndex 17 -IPv4Address 192.168.2.3 -DefaultGateway 192.168.2.1 -AddressFamily IPv4
Store : Active
IPv4Address : 192.168.2.3
IPv6Address :
InterfaceIndex : 17
InterfaceAlias : NicTeam
AddressFamily : IPv4
Type : Unicast
PrefixLength : 32
PrefixOrigin : Manual
SuffixOrigin : Manual
AddressState : Tentative
ValidLifetime : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource : False
Store : Persistent
IPv4Address : 192.168.2.3
IPv6Address :
InterfaceIndex : 17
InterfaceAlias : NicTeam
AddressFamily : IPv4
Type : Unicast
PrefixLength : 32
PrefixOrigin : Manual
SuffixOrigin : Manual
AddressState : Invalid
ValidLifetime : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource : False
When you assign a static IP address to an interface and set the default gateway, it also inserts a default route into the routing table (as it should). (This is why I removed the default route earlier; to show this behavior.)
PS C:\> Get-NetRoute -InterfaceIndex 17
ifIndex RouteMetric DestinationPrefix NextHop Store
------- ----------- ----------------- ------- -----
17 256 0.0.0.0/0 192.168.2.1 Persistent
17 256 255.255.255.255/32 0.0.0.0 Active
17 256 224.0.0.0/4 0.0.0.0 Active
17 256 192.168.2.3/32 0.0.0.0 Active
17 256 0.0.0.0/0 192.168.2.1 Active
[...]