Set an IP address and configure DHCP with PowerShell

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.

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.

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.

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.

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

You should now see that the IP address has changed.

Summary ^

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.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

3+
avatar
Share
4 Comments
  1. Rick Dexter 12 months 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.

    1+
    avatar
    • 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:

      HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\

      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.

      1+

    • Alternatively you can use 

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

       

      0

  2. Jen 9 months ago

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

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

    0

Leave a reply

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

*

© 4sysops 2006 - 2020

CONTACT US

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

Sending

Log in with your credentials

or    

Forgot your details?

Create Account