Skip to main content

Windows Network Management from the command line


PowerShell

Show relevant settings

# IPv4 and IPv6 - Display interfaces sorted by name, index, address family
Get-NetIPInterface | Sort-Object -Property InterfaceAlias,ifIndex,AddressFamily

# Show current DNS configuration
Get-DnsClientServerAddress | Sort-Object -Property InterfaceAlias,InterfaceIndex,AddressFamily

# IPv4 - Display interfaces sorted by metric and alias
Get-NetIPInterface -AddressFamily IPv4 | Sort InterfaceMetric,InterfaceAlias

Reset Ethernet and Wireless interfaces to use DHCP

# Ethernet interfaces use DHCP
Get-NetAdapter -Physical | Where {$_.MediaType -eq "802.3"} | Set-NetIPInterface -Dhcp Enabled
Get-NetAdapter -Physical | Where {$_.MediaType -eq "802.3"} | Set-DnsClientServerAddress -ResetServerAddresses
# Wireless interfaces use DHCP
Get-NetAdapter -Physical | Where {$_.MediaType -eq "Native 802.11"} | Set-NetIPInterface -Dhcp Enabled
Get-NetAdapter -Physical | Where {$_.MediaType -eq "Native 802.11"} | Set-DnsClientServerAddress -ResetServerAddresses

 

DNS Settings Using PowerShell

# Show current DNS configuration
Get-DnsClientServerAddress | Sort-Object -Property InterfaceAlias,InterfaceIndex,AddressFamily

 

Reset Ethernet and Wireless interfaces to use DHCP for DNS servers

# Reset DNS settings on Ethernet interfaces - Use DHCP for DNS servers
Get-NetAdapter -Physical | Where {$_.MediaType -eq "802.3"} | Set-DnsClientServerAddress -ResetServerAddresses

# Reset DNS settings on Wireless interfaces - Use DHCP for DNS servers
Get-NetAdapter -Physical | Where {$_.MediaType -eq "Native 802.11"} | Set-DnsClientServerAddress -ResetServerAddresses

Use specific DNS servers on Ethernet and Wireless interfaces

IPv4

# Reset DNS settings on Ethernet interfaces - Use DHCP for DNS servers
Get-NetAdapter -Physical | Where {$_.MediaType -eq "802.3"} | Set-DnsClientServerAddress -ServerAddresses ("8.8.8.8","1.1.1.1")

# Reset DNS settings on Wireless interfaces - Use DHCP for DNS servers
Get-NetAdapter -Physical | Where {$_.MediaType -eq "Native 802.11"} | Set-DnsClientServerAddress -ServerAddresses ("8.8.8.8","1.1.1.1")

IPv6

# Ethernet interfaces -> use Google + Cloudflare Public DNS
Get-NetAdapter -Physical | Where {$_.MediaType -eq "802.3"} | Set-DnsClientServerAddress -ServerAddresses ("2001:4860:4860::8888","2606:4700:4700::1111")

# Wireless interfaces -> use Google + Cloudflare Public DNS
Get-NetAdapter -Physical | Where {$_.MediaType -eq "Native 802.11"} | Set-DnsClientServerAddress -ServerAddresses ("2001:4860:4860::8888","2606:4700:4700::1111")

Get interface metrics

# IPv4 - Display interfaces sorted by metric and alias
Get-NetIPInterface -AddressFamily IPv4 | Sort InterfaceMetric,InterfaceAlias

# IPv6 - Display interfaces sorted by metric and alias
Get-NetIPInterface -AddressFamily IPv6 | Sort InterfaceMetric,InterfaceAlias

# All - Display interfaces sorted by metric and alias
Get-NetIPInterface | Sort InterfaceMetric,InterfaceAlias

Set interface metrics

The following commands will set Ethernet interfaces to be preferred over wireless interfaces by manipulating the InterfaceMetric of each device. If there are more than one Ethernet and/or Wireless interface on the machine, you may want to adjust these metrics further to provide a more detailed use order.

# Set Ethernet devices interface metric to 11
Get-NetAdapter -Physical | Where {$_.MediaType -eq "802.3"} | Set-NetIPInterface -InterfaceMetric 11

# Set Wireless devices interface metric to 12
Get-NetAdapter -Physical | Where {$_.MediaType -eq "Native 802.11"} | Set-NetIPInterface -InterfaceMetric 12

 


netsh

netsh and firewall

# turn off Windows firewall for all profiles
netsh advfirewall set allprofiles state off

netsh wireless

# show wireless LAN interfaces on the system
netsh wlan show interfaces

# show properties of the wireless LAN drivers on the system
netsh wlan show drivers

# show list of networks visible on the system
netsh wlan show networks

# show more detailed information on visible networks
netsh wlan show networks mode=bssid

# show a list of profiles configured on the systme
netsh wlan show profiles


# connect to an SSID using a Profile
netsh wlan connect ssid=[ssid] name=[profile]


# disconnect all wireless interfaces
netsh wlan disconnect


# PowerShell script to run all of the commands and save the output to a txt file

$outputFile = "$($env:TEMP)\$((Get-Date).ToString('yyyyMMdd_HHmmss'))_netsh_wlan_info_$($env:COMPUTERNAME).output.txt"

$scriptBlock1 = {
  # basic information
  dir env: | Where-Object {$_.Name -Like 'USER*' -Or $_.Name -Like 'COMPUTERNAME' -Or $_.Name -Like 'LOGONSERVER'}
  ipconfig /all
  nslookup google.com
  
  # show wireless LAN interfaces on the system
  netsh wlan show interfaces
  
  # show properties of the wireless LAN drivers on the system
  netsh wlan show drivers
  
  # show list of networks visible on the system
  netsh wlan show networks
  
  # show more detailed information on visible networks
  netsh wlan show networks mode=bssid
  
  # show a list of profiles configured on the systme
  netsh wlan show profiles

  # show the rest of the env:
  dir env:
}

Invoke-Command -ScriptBlock $scriptBlock1 | Out-File -FilePath $outputFile
Write-Output "netsh wlan output saved the following file: $($outputFile)"

Setting IPv4 address using netsh

netsh interface ipv4 show config

# set IPv4 address and dns on an interface using dhcp
netsh interface ipv4 set address name="Ethernet" source=dhcp
netsh interface ipv4 set dns name="Ethernet" source=dhcp

# set IPv4 address on an interface
netsh interface ipv4 set address name="Ethernet" static 10.1.1.84 255.255.255.0 10.1.1.1

# set DNS servers on an interface
netsh interface ipv4 set dns name="Ethernet" static 8.8.8.8 1.1.1.1

#end