Change a Machine’s Subnet Mask with PowerShell Version 2

I recently worked on a project that required me to change the subnet mask on multiple servers at a remote location. The main problem was that many of the servers were still running PowerShell version 2, but luckily PowerShell remoting was enabled on all of them.

This script takes into account multiple network cards and multiple IP addresses being assigned to the same network card. I discovered that the array needed to be reversed in order to assign the primary IP address back to a network card if more than one IP address was assigned to the same NIC, otherwise a secondary IP would be assigned as the primary.

The previous script filters down to network cards that have a specific range of IP addresses which are the ones connected to the LAN because many of the servers also have additional network cards that are connected to an iSCSI network and those shouldn’t be modified.

The following script was used to validate that the settings were indeed changed:

In my opinion, having to work with PowerShell version 2 is less than desirable to say the least, but even in the worst case scenario it’s better than pointing and clicking in the GUI.



  1. Jaap Brasser

    Nice, thanks for sharing the script and the considerations you made while writing the script. I hope switching the primary and secondary IPs at your project did not cause any issues before you identified what happened.

  2. Martyn Jeffrey

    thanks for this, helped alot

  3. umesh

    Set-NetIPAddress -InterfaceAlias “ethernet” -AddressFamily “ipv4” -PrefixLength 20


Leave a Reply to Jaap Brasser Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: