Setting an IP address on a Windows computer is a task most IT professionals have committed to muscle memory by now. Click this, right-click that, click this, type in the IP address, subnet mask, default gateway, and DNS servers…done! It's easy to do, but it's a pain when it's part of a workflow you must automate somehow. To automate setting an IP address, we can use PowerShell to make it quick and easy!

PowerShell includes a few cmdlets that allow us to find a machine's IP address and change it to any configuration we'd like. Let's go through a few scenarios.

Setting a static IP address to DHCP

One of the easiest tasks to perform is to set a NIC to DHCP. Using the Set-NetIPInterface and Remove-NetRoute cmdlets, we can make it happen. We're starting out with an IP configuration that looks like the one below.

Setting the current IP address in the GUI

Setting the current IP address in the GUI

To set this NIC to DHCP, we can use the Set-NetIpInterface command. This will remove the IP address and subnet mask.

Set-NetIPInterface -InterfaceAlias 'Ethernet 2' -Dhcp Enabled

Upon further inspection though, the default gateway is grayed out in TCP/IP properties but still remains. We have to use a different command to remove this, called Remove-NetRoute.

PS> Get-NetIPAddress -InterfaceAlias 'Ethernet 2' | Remove-NetRoute

Are you sure you want to perform this action?
Performing operation "Remove" on Target "NetRoute -DestinationPrefix -InterfaceIndex 8 -NextHop -Store Active"
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): a

This sets the NIC to DHCP with no remnants of the previous default gateway.

Setting a static IP address from DHCP

When going from DHCP to static, the PowerShell cmdlets treat this as a "new" IP address, thus the use of the New-NetIpAddresscmdlet. To use this, you'll need to reference the current IP address and pipe it to New-NetIpAddress using the expected IP address, subnet mask prefix length, and default gateway.

PS C:> Get-NetIpAddress -InterfaceAlias 'Ethernet 2' | New-NetIpAddress  IpAddress -PrefixLength 24 -DefaultGateway

IPAddress : InterfaceIndex : 8 InterfaceAlias : Ethernet 2 AddressFamily : IPv4 Type : Unicast PrefixLength : 24 PrefixOrigin : Manual SuffixOrigin : Manual AddressState : Tentative ValidLifetime : Infinite ([TimeSpan]::MaxValue) PreferredLifetime : Infinite ([TimeSpan]::MaxValue) SkipAsSource : False PolicyStore : ActiveStore

IPAddress : InterfaceIndex : 8 InterfaceAlias : Ethernet 2 AddressFamily : IPv4 Type : Unicast PrefixLength : 24 PrefixOrigin : Manual SuffixOrigin : Manual AddressState : Invalid ValidLifetime : Infinite ([TimeSpan]::MaxValue) PreferredLifetime : Infinite ([TimeSpan]::MaxValue) SkipAsSource : False PolicyStore : PersistentStore

Changing a static IP address

To change an existing static IP address, we have to use the Set-NetIpAddress cmdlet, but it's not quite that easy. To change an existing static IP address, you should first removethe existing one and create a new one. I'll remove the current IP address and remove the default gateway.

Get-NetIPAddress -InterfaceAlias 'Ethernet 2' | Remove-NetRoute
Get-NetIPAddress -InterfaceAlias 'Ethernet 2' | Remove-NetIpAddress

Once I do this, I'll then use the same technique I used above to add a new IP address.

Get-NetIpAddress -InterfaceAlias 'Ethernet 2' | New-NetIpAddress ‑IpAddress -PrefixLength 24 -DefaultGateway

You should now see that the IP address has changed.

Subscribe to 4sysops newsletter!

PS C:\> Get-NetIpAddress -InterfaceAlias 'Ethernet 2'

IPAddress         :
InterfaceIndex    : 8
InterfaceAlias    : Ethernet 2


Changing a NIC IP address and default gateway isn't quite as cut and dry as you'd expect. Due to the way Windows stores IP information, you'll find that completely removing the old IP and default gateway and simply assigning new ones via the cmdlets is the easiest way to accomplish this task.

  1. Avatar
    Rick Dexter 4 years ago

    This is a nicely written blog and it seems like it may confirm my suspicion that it is not possible to programmatically with powershell determine whether or not a client's DNS server is set to manual or obtain via DHCP.  You can only retrieve the DNS servers that are set, but you can't tell whether they were assigned manually or via DHCP.

    I am looking for a way in powershell to determine if the machine has any static settings at all in any of the LAN adapters.  Ideas on how to do this would be appreciated.

    • Avatar
      Leos Marek (Rank 4) 4 years ago

      Nice hint there Rick. It took me a while 🙂 It really seems there is no straight-forward way, not even with WMI. If there is DHCP=Enabled on adapter, you can still have a static DNS. The only way I was able to find out is to go thru records in following Registry key:


      If any of them has value in key NameServer (REG_SZ) this is manually assigned DNS. For DHCP assigned DNS its key DhcpNameServer.

      So you can do a simple script to list adapters where DHCP=Enabled, take this adapter IP and look for Registry path where DhcpIPAddress = yourAdapterIP and check the NameServer or DhcpNameServer values.

      This will be a simple script providing you requested results. Hope this helps, atm I dont have any server around to type the script.

    • Avatar
      Leos Marek (Rank 4) 4 years ago

      Alternatively you can use 

      c:> netsh int ip show dnsservers
      Configuration for interface "Ethernet"
          DNS servers configured through DHCP:
          Register with which suffix:           Primary only
      Configuration for interface "Loopback Pseudo-Interface 1"
          Statically Configured DNS Servers:    None
          Register with which suffix:           None

      and parse the output looking for "Statically Configured…". Tho I guess the Registry way is much easier .)


  2. Avatar
    Jen 4 years ago

    "PS C:> Get-NetIpAddress -InterfaceAlias 'Ethernet 2' | New-NetIpAddress  IpAddress -PrefixLength 24 -DefaultGateway"

    In this bit of code, you forgot the dash in front of "IpAddress;" that might throw some folks off.  Thanks for the write-up!

  3. Avatar
    Curtis J Dove 3 years ago

    How would you do this while invoke-command to a VM?  without causing networking to drop?

  4. Avatar
    Scott Nooteboom 2 years ago

    Thank you for adding the part about removing the existing address, of the countless articles I found about changing the IP via Powershell, you’re the only one who mentioned anything about removing the existing address. I was going in circles until I found your article.

  5. Avatar
    Alon Or 2 weeks ago

    this reg step has to be done or you will get error ” New-NetIpAddress : Inconsistent parameters PolicyStore PersistentStore and Dhcp Enabled ” when you try to set staticIP address

    Set-ItemProperty -Path “HKLM:\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\$((Get-NetAdapter -InterfaceAlias $mynic).InterfaceGuid)” -Name EnableDHCP -Value 0

Leave a reply

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2023


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account