How to replace an existing static IP address with PowerShell

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

  1. Find the index of the interface hosting the current IP address
    Get-NetIPAddress -IPAddress '192.168.100.156'
    

  2. Remove this IP address from the persistent store.
    Remove-NetIPAddress -IPAddress '192.168.100.156'
    

  3. Create a new IP address entry
    New-NetIPAddress -InterfaceIndex 12 -AddressFamily IPv4 -IPAddress '192.168.0.50' -PrefixLength 24
    

  4. 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
    

  5. If the DNS server has changed, set the interface’s DNS server
    Set-DnsClientServerAddress -InterfaceIndex 12 -ServerAddresses 192.168.0.254

More about

An explanation of the Automatic Metric feature for IPv4 routes (Microsoft Support)

IP Routing Table (Microsoft Technet)

Subnets and Subnet Masks (Microsoft Technet)

NetTCPIP cmdlets (Microsoft Docs)
a

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s