Querying and setting network settings with PowerShell

IP Address
Subnet Mask
ICMP Echo Requests
Domain Name System (DNS) Addresses

IP Address

To determine the IP address of a Microsoft Windows system from a command-line interface (CLI) you can open a command prompt window and use the ipconfig command ipconfig /all. If you only need the IPv4 address you can pipe the output into find or findstr. Alternatively, you can use a netsh command.

C:\>ipconfig /all | find "IPv4 Address"
   IPv4 Address. . . . . . . . . . . : 192.168.0.8(Preferred)

C:\>ipconfig | findstr /C:"IPv4"
   IPv4 Address. . . . . . . . . . . : 192.168.0.8

C:\>netsh interface ip show addresses

Configuration for interface "Ethernet"
    DHCP enabled:                         Yes
    IP Address:                           192.168.0.8
    Subnet Prefix:                        192.168.0.0/24 (mask 255.255.255.0)
    Default Gateway:                      192.168.0.1
    Gateway Metric:                       0
    InterfaceMetric:                      25

Configuration for interface "Loopback Pseudo-Interface 1"
    DHCP enabled:                         No
    IP Address:                           127.0.0.1
    Subnet Prefix:                        127.0.0.0/8 (mask 255.0.0.0)
    InterfaceMetric:                      75


C:\>

You can also use a PowerShell cmdlet to view the IP address.

PS C:\> Get-NetIPAddress -AddressFamily IPv4


IPAddress         : 192.168.0.8
InterfaceIndex    : 5
InterfaceAlias    : Ethernet
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Dhcp
SuffixOrigin      : Dhcp
AddressState      : Preferred
ValidLifetime     : 15:58:12
PreferredLifetime : 15:58:12
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : 127.0.0.1
InterfaceIndex    : 1
InterfaceAlias    : Loopback Pseudo-Interface 1
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 8
PrefixOrigin      : WellKnown
SuffixOrigin      : WellKnown
AddressState      : Preferred
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore



PS C:\>

You can get a more concise version showing the interface and address with Get-NetIPAddress -AddressFamily IPv4 | Select-Object InterfaceAlias, IPAddress.

PS C:\> Get-NetIPAddress -AddressFamily IPv4 | Select-Object InterfaceAlias, IPAddress

InterfaceAlias              IPAddress
--------------              ---------
Ethernet                    192.168.0.8
Loopback Pseudo-Interface 1 127.0.0.1


PS C:\>

There is a Set-NetIPAddress command, but that allows you to set parameters associated with an IP address rather than replace an existing IP address with another IP address. Instead, you need to remove the existing address with the Remove-NetIPAddress cmdlet and then set a new address with the New-NetIPAddress cmdlet. You need to specify the network interface for which you are removing the IP address or creating a new IP address. You can determine the approriate index number from the output of the Get-NetIPAddress command. E.g., in the example above the interface was 5, so that is what I would use for the InterfaceIndex value with the Remove-NetIPAddress and New-NetIPAddress commands. When removing the existing IP address, you will be prompted "Are you sure you want to perform this action?" unless you add the -Confirm:$false option. When setting the new IP address, you can specify the prefix length and default gateway address. Setting the IP address to a static IP address will turn off the use of DHCP for obtaining the specified interface address, if it was active. You can also turn off DHCP on a network interface with a Set-NetIPInterface command with the -DHCP Disabled option, e.g., Set-NetIPInterface -InterfaceIndex 5 -Dhcp Disabled. Note: you can check network settings with the Get-NetIPAddress command from a PowerShell window opened with just the privileges granted to a regular Windows account, but to remove an IP address from an interface and create a new IP address, you will need to have opened the PowerShell window as an administrator, which you can do by typing powershell in the Windows search field and then selecting "Run as administrator" rather than "Open" — see image.

PS C:\Windows\system32> Remove-NetIPAddress -InterfaceIndex 5 -Confirm:$false
PS C:\Windows\system32> New-NetIPAddress -InterfaceIndex 5 -IPAddress 192.168.0.31 -PrefixLength 24 -DefaultGateway 192.168.0.1


IPAddress         : 192.168.0.31
InterfaceIndex    : 5
InterfaceAlias    : Ethernet
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Tentative
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : 192.168.0.31
InterfaceIndex    : 5
InterfaceAlias    : Ethernet
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Invalid
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : PersistentStore



PS C:\Windows\system32>

Initially, you may see the address state listed as "Tentative" and "Invalid", but if you issue the Get-NetIPInterface command a couple of minutes later, you should see it listed as "Preferred" on the specified network index. The 127.0.0.1 address is the localhost address.

PS C:\Windows\system32> Get-NetIPAddress -AddressFamily:IPV4


IPAddress         : 192.168.0.31
InterfaceIndex    : 5
InterfaceAlias    : Ethernet
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Preferred
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : 127.0.0.1
InterfaceIndex    : 1
InterfaceAlias    : Loopback Pseudo-Interface 1
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 8
PrefixOrigin      : WellKnown
SuffixOrigin      : WellKnown
AddressState      : Preferred
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore



PS C:\Windows\system32>

Subnet Mask

A subnet mask provides a logical division of an IP network. If systems are in different subnets, you won't be able to directly connect from one system or another or get responses to pings. You can view the subnet mask using a Get-NetIPAddress command such as the one below. You can use -InterfaceIndex or -InterfaceAlias to view the information for a specific network interface.

PS C:\WINDOWS\system32> Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias "Ethernet" | Select-Object Prefixlength

Prefixlength
------------
          28


PS C:\WINDOWS\system32>

Some common Class C prefix length to subnet mask correlations are shown below along with the number of usable host addresses they provide:

Subnet MaskPrefix LengthUsable Hosts Per Subnet
255.255.255.0 24 254
255.255.255.128 25 126
255.255.255.192 26 62
255.255.255.224 27 30
255.255.255.240 28 14
255.255.255.248 29 6
255.255.255.252 30 2

You can set the subnet mask using the Set-NetIPAddress cmdlet. E.g.:

PS C:\WINDOWS\system32> Set-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamily IPv4 -PrefixLength 26
PS C:\WINDOWS\system32> Get-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamily IPv4 | Select-Object Prefixlength

Prefixlength
------------
          26


PS C:\WINDOWS\system32>

ICMP Echo Requests and Replies

You should then be able to ping that address from the PowerShell prompt on the Windows guest operating system, though if you attempt to ping it from the host address, e.g., a terminal window on the Linux host, you won't see a response, because by default the Windows system won't be responding to pings. But you can see that the system is listening on that address, e.g., 192.168.0.31 in the example above, by looking at the Address Resolution Protocol (ARP) table. E.g., on an Ubuntu Linux system, I could use the ip neighbour command to view the ARP table (you can shorten "neighbour" to "neigh"). Note: the status will change from "Delay" to "Reachable" to "Stale" over time.

$ ip neigh | grep '192.168.0.31'
192.168.0.31 dev eno1 lladdr 08:00:27:ad:23:07 DELAY 
$ ip neigh | grep '192.168.0.31'
192.168.0.31 dev eno1 lladdr 08:00:27:ad:23:07 REACHABLE 
$ ip neigh | grep '192.168.0.31'
192.168.0.31 dev eno1 lladdr 08:00:27:ad:23:07 STALE 
$

On Microsoft Windows systems, the Windows Firewall typically blocks incoming ICMP Echo Requests (pings) by default. You can enable the built-in firewall rules using the Enable-NetFirewallRule PowerShell cmdlet to make the system pingable. The Get-NetFirewallRule cmdlet can be used to view the status of firewall rules — they can be used together, e.g., Get-NetFirewallRule -DisplayName "*Echo Request*" | Enable-NetFirewallRule. If you wish to make the system pingable just for IPv4 or IPv6 rather than both, you can use Enable-NetFirewallRule -Name FPS-ICMP4-ERQ-In or Enable-NetFirewallRule -Name FPS-ICMP6-ERQ-In to enable ICMP echo reply responses.

PS C:\Windows\system32> Get-NetFirewallRule -DisplayName "*Echo Request*" | Enable-NetFirewallRule
PS C:\Windows\system32>

You can check on whether echo requests and replies are enabled with a commands like the one below, e.g., Get-NetFirewallRule -Name FPS-ICMP4-ERQ-In | Select-Object DisplayName, Enabled or Get-NetFirewallRule -DisplayName "*Echo Request*" | Select-Object DisplayName, Enabled:

PS C:\Windows\system32> Get-NetFirewallRule -Name FPS-ICMP4-ERQ-In | Select-Object DisplayName, Enabled

DisplayName                                         Enabled
-----------                                         -------
File and Printer Sharing (Echo Request - ICMPv4-In)    True


PS C:\Windows\system32> Get-NetFirewallRule -DisplayName "*Echo Request*" |
>>     Select-Object DisplayName, Enabled

DisplayName                                                  Enabled
-----------                                                  -------
Core Networking Diagnostics - ICMP Echo Request (ICMPv4-In)     True
Core Networking Diagnostics - ICMP Echo Request (ICMPv4-Out)    True
Core Networking Diagnostics - ICMP Echo Request (ICMPv6-In)     True
Core Networking Diagnostics - ICMP Echo Request (ICMPv6-Out)    True
Core Networking Diagnostics - ICMP Echo Request (ICMPv4-In)     True
Core Networking Diagnostics - ICMP Echo Request (ICMPv4-Out)    True
Core Networking Diagnostics - ICMP Echo Request (ICMPv6-In)     True
Core Networking Diagnostics - ICMP Echo Request (ICMPv6-Out)    True
Virtual Machine Monitoring (Echo Request - ICMPv4-In)           True
Virtual Machine Monitoring (Echo Request - ICMPv6-In)           True
File and Printer Sharing (Echo Request - ICMPv4-In)             True
File and Printer Sharing (Echo Request - ICMPv4-Out)            True
File and Printer Sharing (Echo Request - ICMPv6-In)             True
File and Printer Sharing (Echo Request - ICMPv6-Out)            True
File and Printer Sharing (Echo Request - ICMPv4-In)             True
File and Printer Sharing (Echo Request - ICMPv4-Out)            True
File and Printer Sharing (Echo Request - ICMPv6-In)             True
File and Printer Sharing (Echo Request - ICMPv6-Out)            True


PS C:\Windows\system32>

If you want to enable the set of firewall rules associated with file and printer sharing, as you might for systems in a Windows domain, you can enable all of the associated rules with the command Enable-NetFirewallRule -DisplayGroup "File and Printer Sharing".

PS C:\Windows\system32> Enable-NetFirewallRule -DisplayGroup "File and Printer Sharing"
PS C:\Windows\system32>

DNS Addresses

You can view the Domain Name System (DNS) servers for the system using the command Get-DnsClientServerAddress or, if you are only interested in the DNS servers associated with a particular network interface, a command like Get-DnsClientServerAddress -InterfaceIndex 5 (if you change a DHCP-assigned IP address to a static one by removing an IP address and setting a static one, you will need to provide new DNS server addresses). You can use the Set-DnsClientServerAddress to set the DNS server IP addresses.

PS C:\Windows\system32> Set-DnsClientServerAddress -InterfaceIndex 5 -ServerAddresses 192.168.0.5
PS C:\Windows\system32> Get-DnsClientServerAddress -InterfaceIndex 5

InterfaceAlias               Interface Address ServerAddresses
                             Index     Family
--------------               --------- ------- ---------------
Ethernet                             5 IPv4    {192.168.0.5}
Ethernet                             5 IPv6    {}


PS C:\Windows\system32>

Related articles:

  1. Obtaining a command prompt in Windows 11
    Date: August 14, 2023
  2. Obtaining a command prompt in Windows 10
    Date: February 18, 2017
  3. Obtaining a Command Prompt on a Windows 8 System
    Date: February 20, 2017
  4. Setting IP Information From the Command Line
    Date: March 21, 2009