Setting the IP address, subnet mask, default gateway, and DNS of your network card is one of the most basic tasks you're expected to do as an administrator. This guide will teach you how to use PowerShell to get and set the IP address.
Avatar

In most cases, configuring your IP address requires setting at least two things: IP address and subnet mask. If you want to make your life easier, you'll also configure the DNS settings and default gateway.

Get the IP address

You can view the IP address for all your NICs with the PowerShell command Get-NetIPAddress. This provides a significant advantage over the old ways (in the GUI or using ipconfig.exe), as you can see everything in one place.

Without any switches or filtering, Get-NetIPAddress shows a lot of information about the computers' IP configuration. You may see information about APIPA addresses (IP addresses that are automatically assigned to devices on a network when a DHCP server is not available) or various policy stores (more details below) that you don't normally need.

View IP addresses in PowerShell

View IP addresses in PowerShell

Get-NetIPAddress 

A great advantage of PowerShell over other methods (ipconfig.exe or the GUI in the Control Panel) is that you may get as granular as you want and display only the information you need. You can do so for a specific NIC or for all of them.

Filtered information from Get NetIPAddress

Filtered information from Get NetIPAddress

Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias "Ethernet 3" | 
Select-Object InterfaceAlias, IPAddress, PrefixLength,PrefixOrigin 

The command above shows the IP addresses of the NIC "Ethernet 3" on the local computer:

  • Interface name
  • IP address (IPv4 only)
  • Prefix length (subnet mask)
  • Prefix origin—for instance, a manually configured IP or an IP configured via DHCP

You can retrieve the IP addresses from a remote computer using the switch -CimSession:

Get the IP address of a remote computer

Get the IP address of a remote computer

Get-NetIPAddress -CimSession DC2 -AddressFamily IPv4 | 
Select-Object PSComputerName, InterfaceAlias,IPAddress,PrefixLength 

Get the NIC name

When it comes to setting the IP address with PowerShell, the first thing you need to do is get the name of your network interface. If your computer has a single NIC, it usually has the name "Ethernet" or "Ethernet n" (where n is a number. To get the NIC name with PowerShell, you can use this command:

(Get-NetAdapter).InterfaceAlias

For this example, we'll work with a NIC called "Ethernet 3."

NIC Ethernet 3 will be used for the examples in this article

NIC Ethernet 3 will be used for the examples in this article

Set an IPv4 address

Configure IPv4 for a specific NIC

Configure IPv4 for a specific NIC

New-NetIPAddress -InterfaceAlias "Ethernet 3" -AddressFamily IPv4 -IPAddress 10.0.1.222 -PrefixLength 24 -DefaultGateway 10.0.1.1 -Verbose

The command is setting the IP address of "Ethernet 3" to the private address 10.0.1.222 with a subnet mask of 255.255.255.0 and a default gateway of 10.0.1.1, while also providing detailed output of the operation.

A gateway is a network node that connects two different networks, allowing data to flow from one to the other. If you don't know the IP address of the gateway (-DefaultGateway parameter in the command above), you can ask a network administrator or use the PowerShell command below on a computer that has internet access:

Get-NetRoute -DestinationPrefix 0.0.0.0/0 | Select-Object -Property NextHop

This command queries the routing table for the default IPv4 route and extracts the associated gateway's IP address (NextHop). The -DestinationPrefix 0.0.0.0/0 specifies CIDR notation for an IP address and its associated routing prefix. In this context, 0.0.0.0/0 represents all possible IPv4 addresses.

If you run other commands to make additional changes on this NIC immediately after this one, you may encounter an error. A good idea is to wait 2–3 seconds before doing so. If this is part of a script, you may include the good old Start-Sleep -Seconds 5 for good measure.

If you add more than one IP address and specify the same default gateway, you will get an error that the default gateway already exists. Simply skip the switch DefaultGateway to avoid this error.

The PrefixLength defines the local subnet size and is also known as a subnet mask. In this example, the PrefixLength of 24 equals a subnet mask of 255.255.255.0.

While the switch AddressFamily is not required, it is good practice to keep it.

If your computer has only one NIC, you can use the command below to pass the NIC name right in the PowerShell command that sets the IP:

New-NetIPAddress -InterfaceAlias (Get-NetAdapter).InterfaceAlias -AddressFamily IPv4 -IPAddress 10.0.1.222 -PrefixLength 24 -DefaultGateway 10.0.1.1 

Of course, this works for other commands that involve a NIC's name. Just make sure you don't have two or more. In that case, you'd get an error about PowerShell not being able to convert the result (an array) to a string (which is expected for InterfaceAlias).

Set an IPv6 address

Setting an IPv6 address with PowerShell requires essentially the same syntax.

Configure IPv6 for a specific NIC

Configure IPv6 for a specific NIC

New-NetIPAddress -InterfaceAlias "Ethernet 3" -IPAddress 123D::2 -PrefixLength 64 -DefaultGateway 123D::1

This command configures the network interface "Ethernet 3" with a private IPv6 address of 123D::2, a network prefix of 64 bits, and a default gateway address of 123D::1.

Set DNS servers

Set DNS servers for a specific NIC

Set DNS servers for a specific NIC

Set-DnsClientServerAddress -InterfaceAlias "Ethernet 3" -ServerAddresses 10.0.1.12 -Verbose 

The command configures the "Ethernet 3" network interface to use 10.0.1.12 as its DNS server and provides a detailed output of the operation.

You can ask a network administrator for the IP address of your DNS or execute this PowerShell command on a computer with the correct IP configuration:

Get-DnsClientServerAddress -AddressFamily IPv4 | Select-Object InterfaceAlias, ServerAddresses

Alternatively, you can simply run nslookup.

Set-DnsClientServerAddress includes a switch called Validate. It usually fails whenever I run it, so I prefer to test the new settings later on.

You can (and should) add more than one DNS server for the NIC. Usually it's two, and sometimes it can be more. You can do this by separating the DNS servers' IP addresses with a comma:

Set-DnsClientServerAddress -InterfaceAlias "Ethernet 3" -ServerAddresses 10.0.1.12,10.0.1.11,10.0.1.15

If you want to add a new DNS server to the existing list of DNS servers (without removing them), you'll be surprised that there is no command called New-DnsClientServerAddress. Whatever you configured before would be lost. However, here's a little trick that will help you to preserve the existing list of DNS servers: include the list of existing servers, followed by the new address(es) you want to add, like this:

Set-DnsClientServerAddress -InterfaceAlias "Ethernet 3" -ServerAddresses ((Get-DnsClientServerAddress -InterfaceAlias "Ethernet 3").ServerAddresses),10.0.1.20,10.0.1.30. 

Disable and enable IPv6

Microsoft would really like you to not disable IPv6 on any computer running Windows, but you may still decide to do so for whatever reason:

Disable IPv6 for a specific NIC

Disable IPv6 for a specific NIC

Disable-NetAdapterBinding -Name "Ethernet 3" -ComponentID "ms_tcpip6" 

You can do a quick check in the NIC's properties:

In the GUI you can see that IPv6 is disabled for the NIC

In the GUI you can see that IPv6 is disabled for the NIC

You can also verify this with PowerShell:

Verify whether IPv6 is disabled using PowerShell

Verify whether IPv6 is disabled using PowerShell

Get-NetAdapterBinding -InterfaceAlias "Ethernet 3" 

You may notice that IPv6 is disabled (the setting "Enabled" has the value "False").

Once you disable IPv6 and try to add a new IPv6 address, you will get an error:

New-NetIPAddress : Element not found

Before you disable IPv6, you'll need to reenable support for IPv6. You can do so easily by running the following command:

Enable-NetAdapterBinding -Name "Ethernet 3" -ComponentID "ms_tcpip6"

Remove an IP address

Once you have added an IP address to a NIC, the previous one(s) is/are still allocated to the NIC. If you want to remove the old one(s), you can accomplish this with PowerShell using Remove-NetIPAddress, as shown below:

Remove an IP Address

Remove an IP Address

#View current addresses
Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias "Ethernet 3" |Format-Table IPAddress
#Remove the IP address(es)
Remove-NetIPAddress -InterfaceAlias "Ethernet 3" -AddressFamily IPv4 -IPAddress 10.0.1.230,10.0.1.231 -Confirm:$false
#Review the changes
Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias "Ethernet 3" |Format-Table IPAddress 

What is the PolicyStore?

Please note the switch -Confirm:$false in the command you ran to remove an IP address. If you don't use it, you'll be prompted to confirm that you are sure you want to remove the IP address(es). If you're using the command without the switch to skip confirmation in a script, the script will stop and wait for user input.

If you pay attention, you will notice that you get even more prompts than the number of IP addresses you attempt to remove, which may be a bit confusing. Let's see the output of the command to remove an IP address without -Confirm:$false, which skips the prompts.

Without Confirm false you are prompted to delete the IP Address from the ActiveStore and PolicyStore

Without Confirm false you are prompted to delete the IP Address from the ActiveStore and PolicyStore

There are two locations where the IP address is stored: ActiveStore and PersistentStore (as you notice in the screenshot above). The ActiveStore is the store from which the IP addresses are used by Windows for the current session (while Windows is running until you reboot). On the other hand, PersistentStore stores IP address information across restarts. When you restart the computer, Windows copies the saved settings from PersistentStore to ActiveStore.

Normally, you wouldn't care about this, but make sure you delete the setting from the PersistentStore to make sure the IP address you remove will not reappear on your NIC at the next reboot. Using the switch -Confirm:$false removes the IP address from both PolicyStores.

You can also use a specific PolicyStore when you add an IP address. However, if you don't mention it, the IP address is added to both locations (ActiveStore and PersistentStore). This means that the change will apply immediately and will remain the same after the restart.

As a side note, these locations (PolicyStores in Microsoftese) are used in quite a few places in Windows networking. For Windows Firewall, there are even more stores, like the PolicyStore, which stores policies implemented via Group Policy.

Remotely set an IP address

You can also set an IP remotely if PowerShell remoting is enabled on the remote computer. By default, it is enabled in Windows Server. On the remote computer, you can enable PS remoting with this command:

Enable-PSRemoting -Force -SkipNetworkProfileCheck

Learn the different ways to enable PowerShell remoting in our tutorial.

The only thing to keep in mind is that it may take a while for the changes to be reflected (new IP registering in DNS and flushing of the existing cache), or that you may lose connectivity briefly to the target machine.

If I were to add a new IP address to a remote computer, I'd run the same command as before but include the switch -CimSession, followed by the target computer's IP Address or the computer name.

Add an IP address to a remote computer

Add an IP address to a remote computer

Add an IP address to a remote computer

New-NetIPAddress -InterfaceAlias "Ethernet 3" -AddressFamily IPv4 -IPAddress 10.0.1.123 -PrefixLength 24 -Verbose -CimSession MGMT 

In the example above, by specifying the switch -CimSession with the value MGMT, the address 10.0.1.123 was added to the computer with the name "MGMT." You may also notice this if you use the switch -Verbose for detailed output.

Conclusion

In this post, you learned how to avoid clicking and typing numbers in the NIC property fields every time you need to set, add, or delete an IP address or a related setting. If you want to learn more about all the switches and options available with the commands used in this post, you can check the latest Microsoft documentation.

avataravatar
2 Comments
  1. Avatar
    Bo Geitz (Rank 2) 1 month ago

    It’s always amazed me at how slow “Get-NetIPAddress” (or alias ‘gip’) is to output information. This is the only reason I still use ipconfig over Get-NetIPAddress.

    • Avatar Author

      Indeed, some PowerShell commands are slower than their classic counterparts (Get-Computerinfo is another example). I guess whenever WMI queries are performed, speed is not the main thing you’d have in mind. 🙂
      That being said, I prefer the output of Get-NetIpAddress, because I get all the info I care about, and none of the one I don’t (I am rarely concerned about MAC Addresses and the disconnected wired NIC on my laptop). I can format it however I want. And, more vitally, I can use its output as pipeline for other commands.

      But yes, from time to time I use ipconfig. I always go for the more convenient approach, depending on what I need. 🙂

      avatar

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account