Monthly Archive

Categories

IP Default Gateways by cmdlet

Following my recent post on setting the default gateway by using the Win32_NetworkAdapterConfiguration CIM class here’s how you do it using the networking cmdlets

Discover your adapters

Get-NetAdapter

 

Check the default gateway for an adapter

Get-NetIPConfiguration -InterfaceIndex 12 | select InterfaceIndex, IPv4Address, IPv4DefaultGateway

 

Set the default gateway

New-NetRoute -InterfaceIndex 12  -DestinationPrefix '0.0.0.0/0' -NextHop '10.10.54.1'

 

This isn’t as intuitive as using the CIM class

Check the setting

£> Get-NetIPConfiguration -InterfaceIndex 12

InterfaceAlias       : Ethernet
InterfaceIndex       : 12
InterfaceDescription : Microsoft Hyper-V Network Adapter
NetProfile.Name      : Manticore.org
IPv4Address          : 10.10.54.100
IPv6DefaultGateway   :
IPv4DefaultGateway   : 10.10.54.1
DNSServer            : 10.10.54.201

 

To remove the default gateway

Remove-NetRoute -InterfaceIndex 12 -NextHop '10.10.54.1' -Confirm:$false

 

If you leave off –Confirm you’ll be prompted to confirm the action on the active and persistent stores i.e. twice.

 

All of the above cmdlets are part of the NetTCPIP module available on Windows 8/Server 2012 and later.

Comments are closed.