PowerShell for Windows Admins

Feb 22 2016   11:18AM GMT

IP Default Gateways by cmdlet

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Network Adapter
Powershell

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.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: