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 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:


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




By | 2016-10-26T12:52:40+00:00 September 27th, 2016|network, Treasure chest|0 Comments

About the Author:

Stéphane is a dynamic and passionate Cloud and datacenter Microsoft MVP since. He is the founder of the Basel PowerShell user Group (BPUG), the co-founder of the French Speaking PowerShell UserGroup (FRPSUG), author, blogger, and received the community award "PowerShell Hero" from PowerShell.org. Stéphane has implemented microsoft infrastructure solutions in various countries of Europe and is currently working in Basel / Switzerland. Stéphane help his clients to reduce their global infrastructure costs by implementing Microsft infrastructure solutions by combining great products such as System Center, Windows Server, with heavy automation using Windows PowerShell. Stéphane loves languages, Belgium beer, French cheese and French Wine. If any of these topics are of your interest, don't hesitate to come and say hi.

Leave a Reply

%d bloggers like this: