Windows OS Hub
  • Windows Server
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Group Policies
  • Windows Clients
    • Windows 10
    • Windows 8
    • Windows 7
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
  • PowerShell
  • Exchange
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Group Policies
  • Windows Clients
    • Windows 10
    • Windows 8
    • Windows 7
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
  • PowerShell
  • Exchange

 Windows OS Hub / PowerShell / Configuring Network Adapter Settings with PowerShell: IP Address, DNS, Default Gateway, Static Routes

August 3, 2020 PowerShellWindows 10Windows Server 2016

Configuring Network Adapter Settings with PowerShell: IP Address, DNS, Default Gateway, Static Routes

In this article we’ll show you how to configure network adapter parameters in Windows via PowerShell. We will learn how to get and set a static IP address and DNS (nameservers), configure your network interface to obtain the IP configuration from a DHCP server. You can use these cmdlets to configure network in Core/Nano versions of Windows Server, in Hyper-V Server, to change IP settings on remote computers/servers and in your PS scripts.

Previously, the netsh interface ipv4 command was used to configure Windows network settings from the CLI. In PowerShell 3.0 and newer, you can use the built-in PowerShell NetTCPIP module to manage Windows network settings.

To get the list of cmdlets in this module, run the following command:

get-command -module NetTCPIP

Managing WIndows Network Settings with PowerShell NetTCPIP module

This module also includes the Test-NetConnection cmdlet you can use to test TCP port availability on remote computers.

Contents:
  • Managing Network Adapters with PowerShell
  • How to View TCP/IP Network Adapter Settings with PowerShell?
  • Using PowerShell to Set Static IP Address
  • Set-DnsClientServerAddress: Set Primary and Secondary DNS Server Addresses
  • How to Change Static IP Address to DHCP using PowerShell?
  • How to Remotely Change IP Address and DNS settings with PowerShell?

Managing Network Adapters with PowerShell

Display the list of available network interfaces on a computer:

Get-NetAdapter

The cmdlet returns the interface name, its state (Up/Down), MAC address and port speed.

In this example, I have multiple network adapters on my computer (besides the physical connection, Ethernet0, I have some Hyper-V and VMWare Player network interfaces).

Get-NetAdapter - list connected network adapters

You can refer network interfaces by their names or indexes (the Index column). In our example, to select the physical LAN adapter Intel 82574L, use the command:

Get-NetAdapter -Name "Ethernet0"

or:

Get-NetAdapter -InterfaceIndex 8

powershell Get-NetAdapter select NIC by name

You can change the adapter name:

Rename-NetAdapter -Name Ethernet0 -NewName LAN

To disable a network interface, use this command:

Get-NetAdapter -InterfaceIndex 13| Disable-NetAdapter

When you enable an interface, you cannot use its index since it is not assigned yet. You can specify an adapter name or description:

Enable-NetAdapter -InterfaceDescription “Hyper-V Virtual Ethernet Adapter"

Using PowerShell to disable a network adapter

If a VLAN is specified for an adapter, you can display it as follows:

Get-NetAdapter | ft Name, Status, Linkspeed, VlanID

Here is how you can get the information about the network adapter driver used:

Get-NetAdapter | ft Name, DriverName, DriverVersion, DriverInformation, DriverFileName

list network adapter used drivers

The information about physical network adapters (PCI slot, bus, etc.):

Get-NetAdapterHardwareInfo

How to View TCP/IP Network Adapter Settings with PowerShell?

To get current network adapter settings (IP address, DNS, default gateway):

Get-NetIPConfiguration -InterfaceAlias Ethernet0

Get-NetIPConfiguration - Retrieve the IP configuration on WIndows via PowerShell

To display a detailed information about current network adapter TCP/IP configuration, use this command:

Get-NetIPConfiguration -InterfaceAlias Ethernet0 -Detailed

In this case, the assigned network profile (NetProfile.NetworkCategory) of the interface, MTU settings (NetIPv4Interface.NlMTU), whether obtaining an IP address from DHCP is enabled  (NetIPv4Interface.DHCP) and other useful information are displayed.

Get-NetIPConfiguration detailed info

To get the interface IPv4 address only:

(Get-NetAdapter -Name ethernet0 | Get-NetIPAddress).IPv4Address

Using PowerShell to Set Static IP Address

Let’s try to set a static IP address for the NIC. To change an IP address, a subnet mask and default gateway for a network interface use:

New-NetIPAddress –IPAddress 192.168.2.50 -DefaultGateway 192.168.2.1 -PrefixLength 24 -InterfaceIndex 8

You can set an IP address using an array structure (more visually):

$ipParams = @{
InterfaceIndex = 8
IPAddress = "192.168.2.50"
PrefixLength = 24
AddressFamily = "IPv4"
}
New-NetIPAddress @ipParams

You can use the New-NetIPAddress to add a second IP address (alias) to a network adapter.

If the static IP address has already been configured and you want to change it, the Set-NetIPAddress cmdlet is used:

Set-NetIPAddress -InterfaceIndex 8 -IPAddress 192.168.2.90

To disable obtaining an IP address from DHCP for your adapter, run the command:

Set-NetIPInterface -InterfaceAlias Ethernet0 -Dhcp Disabled

To view the routing table, the Get-NetRoute cmdlet is used. To add a new route, use the New-NetRoute cmdlet:

New-NetRoute -DestinationPrefix "0.0.0.0/0" -NextHop "192.168.2.2" -InterfaceIndex 8

To disable the IPv6 protocol for the network adapter:

Get-NetAdapterBinding -InterfaceAlias Ethernet0 | Set-NetAdapterBinding -Enabled:$false -ComponentID ms_tcpip6

Set-DnsClientServerAddress: Set Primary and Secondary DNS Server Addresses

In order to set the primary and secondary DNS server IP addresses in Windows, use the Set-DNSClientServerAddress cmdlet. For example:

Set-DNSClientServerAddress –InterfaceIndex 8 –ServerAddresses 192.168.2.11,10.1.2.11

You can also set nameservers using an array:

$dnsParams = @{
InterfaceIndex = 8
ServerAddresses = ("8.8.8.8","8.8.4.4")
}
Set-DnsClientServerAddress @dnsParams

After changing your DNS settings, you can clear the resolver cache:

Clear-DnsClientCache

How to Change Static IP Address to DHCP using PowerShell?

To allow the computer to obtain a dynamic IP address from the DHCP server for the network adapter, run this command:

Set-NetIPInterface -InterfaceAlias Ethernet0 -Dhcp Enabled

Clear the DNS server settings:

Set-DnsClientServerAddress –InterfaceIndex 8 -ResetServerAddresses

And restart your adapter in order to obtain an IP address automatically from the DHCP server:

Restart-NetAdapter -InterfaceAlias Ethernet0

If you previously had a default gateway configured, remove it:

Set-NetIPInterface -InterfaceAlias Ethernet0| Remove-NetRoute -Confirm:$false

How to Remotely Change IP Address and DNS settings with PowerShell?

You can use PowerShell to remotely change IP address or DNS server settings on multiple remote computers. Suppose, your task is to change DNS settings for all servers in the specific AD container (Organizational Unit). To get the list of computers in the script below, the Get-ADComputer cmdlet is used, and WinRM is used to connect to computers remotely (the Invoke-Command cmdlet):

$Servers = Get-ADComputer -SearchBase ‘OU=Servers,OU=Berlin,OU=DE,DC=woshub,DC=cpm’ -Filter '(OperatingSystem -like "Windows Server*")' | Sort-Object Name
ForEach ($Server in $Servers) {
Write-Host "Server $($Server.Name)"
Invoke-Command -ComputerName $Server.Name -ScriptBlock {
$NewDnsServerSearchOrder = "192.168.2.11","8.8.8.8"
$Adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -ne 'True' -and $_.DNSServerSearchOrder -ne $null}
Write-Host "Old DNS settings: "
$Adapters | ForEach-Object {$_.DNSServerSearchOrder}
$Adapters | ForEach-Object {$_.SetDNSServerSearchOrder($NewDnsServerSearchOrder)} | Out-Null
$Adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -ne 'True' -and $_.DNSServerSearchOrder -ne $null}
Write-Host "New DNS settings: "
$Adapters | ForEach-Object {$_.DNSServerSearchOrder}
}
}

3 comments
1
Facebook Twitter Google + Pinterest
previous post
Adding Users to the Local Admin Group via Group Policy
next post
How to Run a Program as a Different User (RunAs) in Windows 10?

Related Reading

How to Configure and Connect an iSCSI Disk...

January 26, 2021

Preparing Windows for Adobe Flash End of Life...

January 22, 2021

Checking User Logon History in Active Directory Domain...

January 22, 2021

How to Disable/Remove Thumbs.db File on Network Folders...

January 21, 2021

USB Device Passthrough (Redirect) to Hyper-V Virtual Machine

January 15, 2021

3 comments

Shlomi August 24, 2020 - 8:59 pm

Lovely like always!
many thanks

Reply
Manny Ratliff September 11, 2020 - 12:55 pm

Thanks for the great info.
Would you happen to know a way to get the IP from the PC and then set the internet proxy using the 2nd octet from the IP address?
So I have multiple schools and each has a proxy server, so I want to be able to set the proxy based on where that PC is.
Location 1 gives PC IP address of 100.55.50.100, Proxy at this location is 100.55.100.1:9090
Location 2 gives PC IP address of 100.60.50.26, Proxy at this location is 100.60.100.1:9090
The second octet is what changes between locations for both proxy address and IP schema.
I am trying to do a script at login for the user, so that if the device moves locations, it will not need to have the proxy manually re-entered to get back on the internet.

Reply
admin September 17, 2020 - 5:24 pm

I think it will be easier for you to configure the Web Proxy Automatic Detection (WPAD) protocol or Proxy Auto-Configuration (PAC) file to automatically configure proxy setting on client computers.

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange
  • Windows 10
  • Windows 8
  • Windows 7
  • Windows Server 2016
  • Windows Server 2012 R2
  • Windows Server 2008 R2
  • PowerShell
  • VMWare
  • MS Office

Recent Posts

  • How to Configure and Connect an iSCSI Disk on Windows Server?

    January 26, 2021
  • Preparing Windows for Adobe Flash End of Life on December 31, 2020

    January 22, 2021
  • Checking User Logon History in Active Directory Domain with PowerShell

    January 22, 2021
  • How to Disable/Remove Thumbs.db File on Network Folders in Windows?

    January 21, 2021
  • MS SQL Server 2019 Installation Guide: Basic Settings and Recommendations

    January 19, 2021
  • USB Device Passthrough (Redirect) to Hyper-V Virtual Machine

    January 15, 2021
  • Windows 10: No Internet Connection After Connecting to VPN Server

    January 13, 2021
  • Updating the PowerShell Version on Windows

    December 24, 2020
  • How to Enable and Configure User Disk Quotas in Windows?

    December 23, 2020
  • Restoring Deleted Active Directory Objects/Users

    December 21, 2020

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Get-ADUser: Getting Active Directory Users Info via PowerShell
  • Install RSAT Feature on Demand on Windows 10 1809 and Later
  • Managing Printers and Drivers with PowerShell in Windows 10 / Server 2016
  • Get-ADComputer: Find Computer Details in Active Directory with PowerShell
  • How to Create a UEFI Bootable USB Drive to Install Windows 10 or 7?
  • PSWindowsUpdate: Managing Windows Updates from PowerShell
  • PowerShell: Get Folder Sizes on Disk in Windows
Footer Logo

@2014 - 2018 - Windows OS Hub. All about operating systems for sysadmins


Back To Top