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. Ensure DHCP is disabled on this interface.
  3. Get-NetIPInterface -InterfaceIndex 12 | Set-NetIPInterface -Dhcp Disabled
    

    Otherwise, you will get an error message:

    New-NetIPAddress : The object already exists.

     

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

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

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

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

3 thoughts on “How to replace an existing static IP address with PowerShell

  1. 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? :-)

    Like

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

      Like

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 )

Connecting to %s