Configure network settings with PowerShell on Windows: IP Address, DNS, Default Gateway, Static Routes | Ranjan.info

In Windows, you can manage the settings for your network adapter not only from the GUI, but also from the PowerShell command prompt. In this article, we will look at the most important cmdlets that you can use to find out the current IP address of a network adapter, assign a static IP address, assign a DNS server IP, or obtain an IP. Can configure network interface for . Configuration from DHCP server. You can use these cmdlets to change the IP settings of network adapters on Windows 10/11 and Windows Server (or Server Core editions), Hyper-V servers, remote computers, and to configure networking in your PowerShell automation scripts Can

first, the netsh interface ipv4 The command was used to manage network settings from cmd. In PowerShell 3.0 and newer, you can use the built-in nettcpip PowerShell module for managing Windows network settings.

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

get-command -module NetTCPIP

Managing Windows Network Settings with the PowerShell NetTCPIP Module

This module also includes the Test-NetConnection cmdlet which can be used to discover open TCP ports on a remote computer.

Managing network adapter settings through PowerShell

List the available network interfaces on a Windows computer:

Get-NetAdapter

The cmdlet returns the interface name, its status (up/down), MAC address, and port speed.

In this example, I have multiple network adapters on my computer (in addition to physical connections, ethernet0I have some Hyper-V and VMware Player network interfaces).

To display only the enabled physical network interfaces:

Get-NetAdapter -Physical | ? {$_.Status -eq "Up"}

Get-NetAdapter - List connected network adapters

You can view only some network adapter parameters, such as name, speed, status, or MAC address:

Get-NetAdapter |Select-Object name,LinkSpeed,InterfaceOperationalStatus,MacAddress

List NIC MAC Address with PowerShell

You can identify network interfaces by their name or index ( 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 name of the adapter:

Rename-NetAdapter -Name Ethernet0 -NewName LAN

To disable a network interface, use this command:

Get-NetAdapter -Name Ethernet0| Disable-NetAdapter

Enable NIC by its name:

Enable-NetAdapter -Name Ethernet0

Using PowerShell to Disable a Network Adapter

If the network adapter has a configured VLAN number, you can check it:

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

Here’s how you can find information about the network adapter driver you’re using:

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

list network adapter drivers used

List information about the physical network adapter (PCI slot, bus, etc.):

Get-NetAdapterHardwareInfo

Disable IPv6 protocol for the network interface:

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

Disable the NetBIOS protocol for the network interface:

Set-NetAdapterBinding -Name Ethernet0 -ComponentID ms_netbios -AllBindings -Enabled $True

How to get IP address setting with PowerShell?

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

Get-NetIPConfiguration -InterfaceAlias Ethernet0

Get-NetIPConfiguration - Retrieve IP configuration on WIndows via PowerShell

Use the command to display more detailed information about network interface TCP/IP configuration

Get-NetIPConfiguration -InterfaceAlias Ethernet0 -Detailed

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

Get-NetIPConfiguration detailed information

To get the IPv4 interface address only:

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

Return the value of the IP address of the interface only:

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

When copying files to VMs, many administrators have observed poor network performance on Windows Server 2019 with Hyper-V roles enabled. In this case, reverting the TCP stack settings back to the settings used in Windows Server 2016 will help resolve the issues:

Set-NetTCPSetting -SettingName DatacenterCustom,Datacenter -CongestionProvider DCTCP
Set-NetTCPSetting -SettingName DatacenterCustom,Datacenter -CwndRestart True
Set-NetTCPSetting -SettingName DatacenterCustom,Datacenter -ForceWS Disabled

Display a list of network protocols that can be enabled or disabled for the network adapter:

Get-NetAdapterBinding -Name ethernet0 -IncludeHidden -AllBindings

Get-NetAdapterBinding View enabled network protocols

Name                           DisplayName                                        ComponentID          Enabled
----                           -----------                                        -----------          -------
Ethernet                       File and Printer Sharing for Microsoft Networks    ms_server            True
Ethernet                       NetBIOS Interface                                  ms_netbios           True
Ethernet                       Microsoft LLDP Protocol Driver                     ms_lldp              True
Ethernet                       Microsoft NDIS Capture                             ms_ndiscap           True
Ethernet                       Internet Protocol Version 4 (TCP/IPv4)             ms_tcpip             True
Ethernet                       Microsoft RDMA - NDK                               ms_rdma_ndk          True
Ethernet                       Microsoft Network Adapter Multiplexor Protocol     ms_implat            False
Ethernet                       Link-Layer Topology Discovery Mapper I/O Driver    ms_lltdio            True
Ethernet                       NDIS Usermode I/O Protocol                         ms_ndisuio           True
Ethernet                       Point to Point Protocol Over Ethernet              ms_pppoe             True
Ethernet                       Link-Layer Topology Discovery Responder            ms_rspndr            True
Ethernet                       Internet Protocol Version 6 (TCP/IPv6)             ms_tcpip6            True
Ethernet                       Hyper-V Extensible Virtual Switch                  vms_pp               False
Ethernet                       WFP Native MAC Layer LightWeight Filter            ms_wfplwf_lower      True
Ethernet                       Client for Microsoft Networks                      ms_msclient          True
Ethernet                       Npcap Packet Driver (NPCAP)                        INSECURE_NPCAP       True
Ethernet                       WINS Client(TCP/IP) Protocol                       ms_netbt             True
Ethernet                       Bridge Driver                                      ms_l2bridge          True
Ethernet                       WFP 802.3 MAC Layer LightWeight Filter             ms_wfplwf_upper      True
Ethernet                       QoS Packet Scheduler                               ms_pacer             True

Set Static IP Address on Windows Using PowerShell

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

Get-NetAdapter -Name Ethernet0| New-NetIPAddress –IPAddress 192.168.2.50 -DefaultGateway 192.168.2.1 -PrefixLength 24

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

If a static IP address is already configured and needs to be changed, use set-netipaddress Cmdlet:

Set-NetIPAddress -InterfaceAlias Ethernet0 -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

Remove static IP address:

Remove-NetIPAddress -IPAddress "xxx.xxx.xxx.xxx"

Set DNS Server IP Addresses with PowerShell in Windows

To set preferred and alternate DNS server IP addresses in Windows, use Set-DNSClientServerAddress cmdlet. For example:

Set-DNSClientServerAddress –InterfaceIndex 8 –ServerAddresses 192.168.2.11,10.1.2.11

You can also specify DNS nameserver IPs using an array:

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

After changing DNS settings, you can flush the DNS resolver cache (equivalent to ipconfig /flushdns ,

Clear-DnsClientCache

Display DNS Cache Contents in Windows ::
Get-DnsClientCache

Managing Routing Tables with PowerShell

get-netroot The cmdlet is used to display the routing table.

Get the default gateway route for a physical network interface in Windows:

Get-NetAdapter -Physical | ? {$_.Status -eq "Up"}| Get-netroute| where DestinationPrefix -eq "0.0.0.0/0"

PowerShell: Get Default Gateway Route

To add a new route, use new-netroot Cmdlet:

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

This command adds a permanent route to the routing table (eg route -p add, If you want to add a temporary route, add -PolicyStore "ActiveStore" Option. This path will be removed after restarting Windows.

Delete route from routing table:

Remove-NetRoute -NextHop 192.168.0.1 -Confirm:$False

PowerShell: Change Adapter from Static IP Address to DHCP

To configure your computer to obtain dynamic IP addresses for network adapters from the DHCP server, run this command:

Set-NetIPInterface -InterfaceAlias Ethernet0 -Dhcp Enabled

Clear DNS server settings:

Set-DnsClientServerAddress –InterfaceAlias Ethernet0 -ResetServerAddresses

and restart your network adapter to automatically obtain an IP address 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

If you need to reset all IPv4 settings for the computer’s network interface and switch to obtain a dynamic IP address from DHCP, use the following script:

$IPType = "IPv4"
$adapter = Get-NetAdapter | ? {$_.Status -eq "up"}
$interface = $adapter | Get-NetIPInterface -AddressFamily $IPType
If ($interface.Dhcp -eq "Disabled") {
If (($interface | Get-NetIPConfiguration).Ipv4DefaultGateway) {
$interface | Remove-NetRoute -Confirm:$false
}
$interface | Set-NetIPInterface -DHCP Enabled
$interface | Set-DnsClientServerAddress -ResetServerAddresses
}

Change DNS and IP Addresses Remotely on Multiple Computers with PowerShell

You can use PowerShell to remotely change the IP address or DNS server settings on multiple remote computers. Let’s say your task is to change the DNS settings on all Windows Server hosts in a specific AD Organizational Unit (OU). The following script uses the Get-ADComputer cmdlet to retrieve a list of computers from Active Directory and then connects to remote computers via WinRM (the Invoke-Command cmdlet is used):

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

Leave a Comment