• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Think PowerShell

Think PowerShell

PowerShell for IT Pros

  • About
    • Who I Am
    • Why I Blog
    • Privacy Policy
  • Resources
    • More PowerShell Sites
    • Post Series Index
  • Get Help
  • Show Search
Hide Search

Configure Static IP Address or DHCP

Aaron Rothstein · February 23, 2016 · 3 Comments

Windows 10 network connections screenshot.

The second post in my PowerShell Beginner series, “Daily Tasks, The PowerShell Way“. Learn how to get network connection information and configure a static IP or DHCP using PowerShell.

Are you connected?

In my previous post we covered how to use Test-NetConnection for network connectivity troubleshooting. If you connected to a DHCP enabled network, you will likely have successful tests without any action on your part. However, if your network environment requires static configuration of IP addresses and DNS servers for servers and/or workstations, you need to configure the correct network settings for a newly provisioned computer.

Get network connection details

First, let’s see what’s connected. Get-NetAdapter will return basic network information. We see network connection “Ethernet0” is up with a speed of 1Gbps.

PS C:\Windows\system32> Get-NetAdapter

Name         InterfaceDescription                       ifIndex    Status    MacAddress           LinkSpeed
----         --------------------                       -------    ------    ----------           ---------
Ethernet0    Intel(R) 82574L Gigabit Network Conn...    5          Up        00-0C-29-01-81-15    1 Gbps

We can use the cmdlet Get-NetIPInterface to get a summary table of our network interfaces and their connection state. Because I’m only concerned about IPv4, I include the parameter -AddressFamily IPv4. We can see that I have one physical interface connected “Ethernet0”, and it is currently configured for DHCP.

PS C:\Windows\system32> Get-NetIPInterface -AddressFamily IPv4

ifIndex    InterfaceAlias                 AddressFamily    NlMtu(Bytes)    InterfaceMetric    Dhcp        ConnectionState    PolicyStore
-------    --------------                 -------------    ------------    ---------------    ----        ---------------    -----------
5          Ethernet0                      IPv4             1500            10                 Enabled     Connected          ActiveStore
1          Loopback Pseudo-Interface 1    IPv4             4294967295      50                 Disabled    Connected          ActiveStore

We can use the Get-NetIPConfiguration cmdlet to return IP information for “Ethernet0”. We learn the IPv4 address, the default gateway, and DNS servers. One thing that is absent from this result set is subnet mask, hopefully that will be added in the future. However, we can get the subnet mask as a prefix value if we run Get-NetIPAddress. You can see PrefixLength is 24 (/24 or 255.255.255.0).

PS C:\Windows\system32> Get-NetIPConfiguration -InterfaceAlias "Ethernet0"

InterfaceAlias : Ethernet0
InterfaceIndex : 5
InterfaceDescription : Intel(R) 82574L Gigabit Network Connection
NetProfile.Name : Network 2
IPv4Address : 192.168.1.130
IPv6DefaultGateway : 
IPv4DefaultGateway : 192.168.1.2
DNSServer : 192.168.1.2

PS C:\Windows\system32> Get-NetIPAddress -InterfaceAlias "Ethernet0" -AddressFamily IPv4

IPAddress : 192.168.1.130
InterfaceIndex : 5
InterfaceAlias : Ethernet0
AddressFamily : IPv4
Type : Unicast
PrefixLength : 24
PrefixOrigin : Dhcp
SuffixOrigin : Dhcp
AddressState : Preferred
ValidLifetime : 00:16:19
PreferredLifetime : 00:16:19
SkipAsSource : False
PolicyStore : ActiveStore

Set static IP address

You may want to set a static IP if your computer is a server or need to set static IPs in an environment without DHCP. You can do this using the New-NetIPAddress cmdlet. This is equivalent to opening the Properties of a network connection and manually configuring the adapter.

PS C:\Windows\system32> New-NetIPAddress -InterfaceAlias "Ethernet0" -AddressFamily IPv4 -IPAddress 192.168.1.230 -PrefixLength 24 -DefaultGateway 192.168.1.2


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

Set DNS servers manually

If you configure a static IP address, you will also need to manually configure DNS servers for the connection. Use Set-DnsClientServerAddress. You can specify multiple DNS servers separated by commas.

PS C:\Windows\system32> Set-DnsClientServerAddress -InterfaceAlias Ethernet0 -ServerAddresses 192.168.1.2,8.8.8.8,8.8.4.4

PS C:\Windows\system32>

If you manually inspect the adapter, you can verify the static IP information and DNS servers are set.

Windows network ipv4 properties.

Reset network connection to DHCP

My demo computer is working with a static IP configuration, but now that I’m done I want to revert it back to its original DHCP configuration. To do that in PowerShell, I need to do three things:

  1. Set the connection back to “Obtain an IP address automatically”
  2. Remove the static default gateway
  3. Set the connection back to “Obtain DNS server address automatically”

I can do this by running a sequence of three cmdlets, Set-NetIPInterface, Remove-NetRoute, and Set-DNSClientServerAddress. I will also incorporate Get-NetIPInterface and use piping to streamline the command input.

# Return network interface to a variable for future use
$interface = Get-NetIPInterface -InterfaceAlias "Ethernet0" -AddressFamily IPv4

# Remove the static default gateway
$interface | Remove-NetRoute -AddressFamily IPv4 -Confirm:$false

# Set interface to "Obtain an IP address automatically"
$interface | Set-NetIPInterface -Dhcp Enabled

# Set interface to "Obtain DNS server address automatically"
$interface | Set-DnsClientServerAddress -ResetServerAddresses

Next steps

  1. The next time you are provisioning a server or workstation, use PowerShell to configure a static IP and DNS servers.
  2. If the computer ultimately should use DHCP, use PowerShell to remove the static configuration and revert to DHCP.

Reference

  • technet.microsoft.com
    • Net TCP/IP Cmdlets in Windows PowerShell

General Beginner, Network

Reader Interactions

Comments

  1. Ray says

    November 8, 2017 at 2:12 pm

    I have a Win10 Home pc that I want to add to my home network. I have reserved IP address 192.168.1.115 on my router for that MAC address. It keeps reverting to 192.168.15.2 and gateway 192.168.15.1.

    I have done the standard ipconfig /release…/renew, even forcing winsock and int ipv4/v6 resets; and I uninstalled the NIC, all to no avail.

    Any ideas?

    Reply
    • Aaron Rothstein says

      November 10, 2017 at 7:50 pm

      That sounds like an odd issue. You don’t have more than one DHCP server running on your network, do you? And you verified the adapter is configured for DHCP?

  2. Stephen Shedden says

    November 28, 2022 at 9:30 pm

    Great Page. Thank you.

    Reply

Leave a Reply Cancel reply

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

Primary Sidebar

Aaron

Think PowerShell

Copyright © 2025 · Monochrome Pro on Genesis Framework · WordPress · Log in