Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 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}
}
}

7 comments
3
Facebook Twitter Google + Pinterest
previous post
How to Allow Non-Admin Users to Start/Stop Windows Service?
next post
Ubuntu/Mint/Kali Boots to Initramfs Prompt in BusyBox

Related Reading

Configure User’s Folder Redirection with Group Policy

February 3, 2023

Disable Built-in PDF Viewer in Microsoft Edge

February 3, 2023

Join a Windows Computer to an Active Directory...

February 2, 2023

Using Previous Command History in PowerShell Console

January 31, 2023

How to Install the PowerShell Active Directory Module...

January 31, 2023

7 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
D.Visan May 22, 2021 - 9:41 pm

Cheers!

Reply
Luciano Pavearoadi June 17, 2021 - 10:16 pm

Double thumbs up, well done.

Reply
Zeke Davis September 21, 2021 - 5:53 pm

Any Idea how to modify the Connection specific DNS suffix for a network adapter via powershell if possible?

Reply
Duck January 30, 2022 - 6:01 pm

Wow, amazing work!
Was wondering if you know a way to set a static ip for a network adapter without it disconnecting UDP/TCP connections?

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Configure User’s Folder Redirection with Group Policy

    February 3, 2023
  • Using Previous Command History in PowerShell Console

    January 31, 2023
  • How to Install the PowerShell Active Directory Module and Manage AD?

    January 31, 2023
  • Finding Duplicate E-mail (SMTP) Addresses in Exchange

    January 27, 2023
  • How to Delete Old User Profiles in Windows?

    January 25, 2023
  • How to Install Free VMware Hypervisor (ESXi)?

    January 24, 2023
  • How to Enable TLS 1.2 on Windows?

    January 18, 2023
  • Allow or Prevent Non-Admin Users from Reboot/Shutdown Windows

    January 17, 2023
  • Fix: Can’t Extend Volume in Windows

    January 12, 2023
  • Wi-Fi (Internet) Disconnects After Sleep or Hibernation on Windows 10/11

    January 11, 2023

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Installing RSAT Administration Tools on Windows 10 and 11
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • How to Hide Installed Programs in Windows 10 and 11?
  • Managing Printers and Drivers with PowerShell in Windows 10 / Server 2016
  • How to Create a UEFI Bootable USB Drive to Install Windows 10 or 7?
  • PowerShell: Get Folder Sizes on Disk in Windows
  • Deploy PowerShell Active Directory Module without Installing RSAT
Footer Logo

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


Back To Top