06.12.2024

Configure Windows with multiple IP Addresses via PowerShell

In some cases, for example, on web servers, it is required to configure more than one IP address on one network adapter. If this is one server and it has one or two network cards and a small number of additional IP addresses, then, as a rule, configuration through the graphical interface will not cause problems. But what if there are more than ten servers, and each has several network adapters with a whole bunch of IP addresses? In this case, setting up via the graphical interface will take a lot of time. In this case, Windows automation tools PowerShell will come to the rescue. Next we will consider the configuration process for the Windows Server 2016/2019 OS.

First you need to run Powershell as an administrator. You can do this by right-clicking on the "Start" menu and selecting "Windows PowerShell (Admin)" in case of an English version of OS:

Screenshot №1. Start PowerShell as Administrator

Next, a PowerShell window will open. Now you need to determine on which interface you need to add the required IP addresses.

Using below command, we determine the required network interface:

Get-NetAdapter

Screenshot № 2. Determine network adapters for setup

In this case, one network adapter named "Ethernet", and we will use it. Let's first find out which IPs are already configured on this adapter using the command :

Get-netIpAddress -ifalias Ethernet -addressFamily Ipv4 | ft

Screenshot № 3. Find out which IPs are already configured

To add a new address, for example 192.168.10.10, you must use the following command:

New-NetIPAddress -addressfamily Ipv4 -IPAddress 192.168.10.10 -PrefixLength 24 -InterfaceAlias “Ethernet” -SkipAsSource $True

Check the result by running this command below:

Screenshot № 4. Result by running command New-IPAddress

As you can see, the adapter has been assigned another IPv4 address — 192.168.10.10. If you want outgoing traffic to be sent from the added IP address, you need to change parameter as below :

Screenshot № 5. Set outgoing traffic to be sent from the added IP address

If you need to add a larger number of addresses, for example 5, then you can use the script. Using an array of numbers, for example, from 11 to 16 and a foreach-object loop, add 5 more IP addresses:

11..16 | foreach-object {New-NetIpAddress -ifalias Ethernet -AddressFamily IPv4 -PrefixLength 24 -IPAddress "192.168.10.$_" -verbose}

Screenshot № 6. Adding multiple IP by script

After executing the command, check with the already familiar command:

Screenshot № 7. Result after executing script

Conclusion: In this article, we looked at the possibilities of using PowerShell to automate the assignment of multiple IP addresses on a single network interface.