Starting with Windows Server 2012, you can now handle directly the IP configuration with PowerShell cmdlets.
Unfortunately, there is no cmdlet which directly updates an already existing IP address. You have to use four of them.
Please be aware that you will disconnect yourself from the computer if you do this operation remotely. This won’t happen if you use PowerShell Direct or a VM console.
Main steps
- Find the index of the interface hosting the current IP address
Get-NetIPAddress -IPAddress '192.168.100.156'
- Ensure DHCP is disabled on this interface.
- Remove this IP address from the persistent store.
Remove-NetIPAddress -IPAddress '192.168.100.156'
- Create a new IP address entry
New-NetIPAddress -InterfaceIndex 12 -AddressFamily IPv4 -IPAddress '192.168.0.50' -PrefixLength 24
- If the gateway is different from the former one, remove it, and set a new one
Remove-NetRoute -InterfaceIndex 12 -NextHop 192.168.100.1 New-NetRoute -InterfaceIndex 12 -DestinationPrefix '0.0.0.0/0' -AddressFamily IPv4 -NextHop '192.168.0.254' -RouteMetric 0
- If the DNS server has changed, set the interface’s DNS server
Set-DnsClientServerAddress -InterfaceIndex 12 -ServerAddresses 192.168.0.254
Get-NetIPInterface -InterfaceIndex 12 | Set-NetIPInterface -Dhcp Disabled
Otherwise, you will get an error message:
New-NetIPAddress : The object already exists.
More about
An explanation of the Automatic Metric feature for IPv4 routes (Microsoft Support)
IP Routing Table (Microsoft Technet)
Hey Luke
Thanks for the article!
I’ve tried this, but I run in to the following issue:
With the
Get-Netipaddress -InterfaceIndex $adapterIndex | Remove-NetIPAddress -Confirm:$false
followed by
New-NetIPAddress -IPAddress $ip -InterfaceIndex $adapterIndex -PrefixLength $subnet -DefaultGateway $gateway -AddressFamily IPv4
I get the following error:
New-NetIPAddress : The object already exists.
At line:373 char:9
+ New-NetIPAddress -IPAddress $ip -InterfaceIndex $adapterIndex …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (MSFT_NetIPAddress:ROOT/StandardCimv2/MSFT_NetIPAddress) [New-NetIPAddress], CimException
+ FullyQualifiedErrorId : Windows System Error 5010,New-NetIPAddress
The variables contain the correct, corresponding values.
Any idea why this happens, and any proper solutions? :-)
LikeLike
Hi Terji,
Thanks for reaching out about this error.
This happens when DHCP is enabled on the interface.
It automatically creates another IP address entry before you can create the new one.
I updated the article with the command to disable DHCP on the interface.
LikeLike
Hey Luke
Thanks for the quick reply! It seems to be working now, so thanks for the update. :-)
LikeLike