I have been automating the provisioning of Hyper-V hosts using SCVMM and PowerShell, and one of the tasks that I had to do involved to set an use powershell to set an ip address , but also use powershell to set an ip subnetmask , and to set the a gateway.

Use Powershell ip address:

I knew the existence of Set-NetIpAddress and the New-NetIpAddress cmdlets but I have never used them before. Now that i wanted to set to use powershell ip address, it was the perfect opportunity to answer the question: How to set an ip address using powershell? So let’s to try them out.

Naturally I tried to use the ‘set-NetIpAddress’, but I kept having the following error:

I soon noticed that the Set-NetIpAddress cmdlet wasn’t really working the way I was expecting.

If you have a closer look at the set-NetIpAddress help on msdn, we will see the following:

The Set-NetIPAddress cmdlet modifies IP address configuration properties of an existing IP address.

To create an IPv4 address or IPv6 address, use the New-NetIPAddress cmdlet.

You can set an ip address using New-NetIpAddress indeed, but there is a caveat. If one is already set, you will actually set a new primary ip address, and the one that existed before, will be shifted to be a secondary IP address. If you call the cmdlet again, you will shift that first IP address to the third position, without actually replacing the ip address. This goes for the gateway address as well actually.

In the end, if you want to replace an ip address with a new one using powershell (or if you want to change a gateway using powershell) you will need to remove the old ip address (or gateway) first, and then set an powershell ip address.

Now, to remove an ip address there is a handy little cmdlets called ‘remove-netipaddress’, which works just as you would expect (unlike set-ipaddress) but to remove the gateway, it is is a bit trickier. In order to remove the gateway using powershell, we will have to remove the route going there. If the gateway you want to remove is 192.168.2.1 you will have to remove the route using remove-netroute as follow:

I ended up writing the function below which, which takes care of all of this additional logic, and give us the possibility to ‘simply’, replace an ip address using powershell.

Read more about powershell ip address:

While doing some researches on automating IP related tasks, I wrote down the following interesting links. If you have a some time, you could go and have visit ^^

Love the functions Test-IsValidIP from Boe prox on the powershell magazine:

http://www.powershellmagazine.com/2012/06/20/boe-prox-favorite-powershell-tips-tricks/

Read these other ressources to learn about this and see this from another angle:

https://blogs.technet.microsoft.com/bruce_adamczak/2013/01/23/windows-2012-core-survival-guide-default-gateway-settings/

http://www.adminarsenal.com/admin-arsenal-blog/using-powershell-to-set-static-and-dhcp-ip-addresses-part-1/

https://blogs.technet.microsoft.com/heyscriptingguy/2013/01/24/use-powershell-to-change-ip-behavior-with-skipassource/