How to switch between static IP address and DHCP with PowerShell

Starting with Windows Server 2012, you can now handle directly the IP configuration with PowerShell cmdlets.

However, despite the way to go from a DHCP IP address to a static IP address is not easy to find, if you know it, it is pretty easy to understand.

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.

How to switch from DHCP to Static IP

How it works

When a network interface is configured for DHCP , it creates itself an entry in the Active Store with an IP address, mask and routing gateway.

On the other hand, when you set the a network interface to Manual, you have to create yourself the entry in the Persistent Store, add yourself the gateway entry to the routing table and configure yourself the DNS client.


In the following examples, I will only focus on one interface (with Index 15 in my case) and only focus on IPv4. However, the same method applies to IPv6.

Main steps

  1. Find the index of all interfaces for which DHCP is enabled
    Get-NetIPInterface -AddressFamily IPv4 -Dhcp Enabled

  2. Get the current IP address and mask
    Get-NetIPAddress -InterfaceIndex 15 -AddressFamily IPv4

  3. Get the current gateway and DNS server list
    Get-NetIPConfiguration -InterfaceIndex 15

  4. Disable DHCP for this interface.
    This step is optional, because the static IP address takes precedence over the DHCP configuration.

    Set-NetIPInterface -InterfaceIndex 15 -AddressFamily IPv4 -Dhcp Disabled

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

  6. Set the gateway for the interface
    New-NetRoute -InterfaceIndex 15 -DestinationPrefix '' -AddressFamily IPv4 -NextHop '' -RouteMetric 0
  7. Set the interface’s DNS server
    Set-DnsClientServerAddress -InterfaceIndex 15 -ServerAddresses

Example of code for automation

$CurrentIP = Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Dhcp
$CurrentGateway = (Get-NetIPConfiguration -InterfaceIndex $CurrentIP.InterfaceIndex).IPv4DefaultGateway.NextHop
$CurrentDNS = ((Get-NetIPConfiguration -InterfaceIndex$CurrentIP.InterfaceIndex).DNSServer |
        Where-Object AddressFamily -eq 2).ServerAddresses

Set-NetIPInterface -InterfaceIndex $CurrentIP.InterfaceIndex -Dhcp Disabled

$Params = @{
    IPAddress      = $CurrentIP.IPAddress
    InterfaceAlias = $CurrentIP.InterfaceAlias
    Type           = $CurrentIP.Type
    PrefixLength   = $CurrentIP.PrefixLength

New-NetIPAddress @Params

$Params = @{
    InterfaceIndex    = $CurrentIP.InterfaceIndex
    DestinationPrefix = ''
    AddressFamily     = 'IPv4'
    NextHop           = $CurrentGateway

New-NetRoute @Params

Set-DnsClientServerAddress -InterfaceIndex $CurrentIP.InterfaceIndex -ServerAddresses $CurrentDNS

How to switch from static IP to DHCP

This operation is much simpler.

  1. Configure the DNS client to get the DNS server list from the DHCP configuration (optional)
    Set-DnsClientServerAddress -InterfaceIndex 15 -ResetServerAddresses

  2. Remove the current Gateway entry from the Persistent Store of the routing table (optional but cleaner).
    Get-NetRoute -InterfaceIndex 15 -AddressFamily IPv4 -PolicyStore PersistentStore | Remove-NetRoute

  3. If you disabled DHCP on the interface, you have to enable it again.
    Set-NetIPInterface -InterfaceIndex 15 -Dhcp Enabled

    Otherwise, remove this IP address from the persistent store.

    Remove-NetIPAddress -IPAddress ''

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)

Dynamic Host Configuration Protocol (DHCP) (Microsoft Docs)

NetTCPIP cmdlets (Microsoft Docs)

Leave a Reply

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

You are commenting using your 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