Using PowerShell to Get or Set NetworkAdapterConfiguration-View and Change Network Settings Including DHCP, DNS, IP Address and More (Dynamic AND Static) Step-By-Step

I was looking for a really good and thorough post on network configuration via PowerShell and could not find what I was looking for.  There were many out there but none of them gave me the complete story.  DNS is a bit more complicated so they left off DNS Sad smile.  So,  I decided to create one myself.  I hope you find it useful. The Get-WmiObject in PowerShell gets instances of Windows Management Instrumentation (WMI) classes or information about the available classes.  Syntax details of Get-WmiObject in PowerShell can be found at https://technet.microsoft.com/en-us/library/dd315295.aspx 

You can get additional information on the Win32_NetworkAdapterConfiguration from the developer reference at https://msdn.microsoft.com/en-us/library/windows/desktop/aa394217(v=vs.85).aspx.  Another location to look for PowerShell specific tasks would be Performing Networking Tasks

Getting Started – Launch PowerShell

The first lesson I share on this topic has to be run PowerShell with elevated permissions.  You will need to do that to follow the instructions as we get through this tutorial.  To do this, see How to Run Any Program Including PowerShell with Elevated Privileges (aka Administrator Permissions).

Listing IP Addresses for a Computer

To get all IP addresses in use on the local computer, use the following command:

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Format-Table -Property IPAddress

image

The output of this command differs from most property lists, because values are enclosed in braces

IPAddress
---------
{192.168.168.175, fe80::c994:2b82:e28c:b772}
{192.168.1.3, fe80::293e:b6f4:369b:a620}

The IPAddress property for each network adapter is actually an array. The braces in the definition indicate that IPAddress is not a System.String value, but an array of System.String values which means it can contain multiple IP Addresses for each network adapter.  In my case, it is showing both an IPv4 address and an IPv6 address.  If I had more IP Addresses assigned to the NIC it would show all of them.  Notice in my command I am using the –Filter to only show NICs where the IPEnabled Property=True.  I actually have many more networks on this machine but I am only showing the networks that are enabled.  The Format-Table part of the command tells PowerShell to output only the IPAddress property and put them in a table format.

An example of a NIC that has multiple IP addresses might look something like the following:

image

IPAddress
---------
{192.168.168.175, 192.168.1.155, 10.10.12.89, fe80::c994:2b82:e28c:b772}
{192.168.1.3, fe80::293e:b6f4:369b:a620}

Notice in this example I have 4 IP addresses in my first NIC; 3 are IPv4 and 1 is IPv6 which are:

  • 192.168.168.175
  • 192.168.1.155
  • 10.10.12.89
  • fe80::c994:2b82:e28c:b772

Display detailed IP configuration data for each network adapter

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName .

image

In this output you can see more detailed configuration for each adapter.  Note that I am still only showing the Enabled NICs but I am showing more properties.

DHCPEnabled      : False
IPAddress        : {192.168.168.175, 192.168.1.155, 10.10.12.89, fe80::c994:2b82:e28c:b772}
DefaultIPGateway :
DNSDomain        :
ServiceName      : e1express
Description      : Intel(R) 82566MM Gigabit Network Connection
Index            : 7

DHCPEnabled      : True
IPAddress        : {192.168.1.3, fe80::293e:b6f4:369b:a620}
DefaultIPGateway : {192.168.1.1}
DNSDomain        : home
ServiceName      : netw5v64
Description      : Intel(R) Wireless WiFi Link 4965AGN
Index            : 12

I can filter this down to only show the things I want by using a Select-Object or a formatting cmdlet.

The default display for the network adapter configuration object is a very reduced set of the available information. For in-depth inspection and troubleshooting, use Select-Object or a formatting cmdlet, such as Format-List, to specify the properties to be displayed.

If you are not interested in IPX or WINS properties—probably the case in a modern TCP/IP network—you can use the ExcludeProperty parameter of Select-Object to hide properties with names that begin with "WINS" or "IPX:".  You can also select ALL properties by using a wildcard for the property name. 

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Select-Object -Property [a-z]* -ExcludeProperty IPX*,WINS*

In this case, you can see all the properties except those that begin with WINS or IPX:.

image

Here is the output for the first adapter:

DHCPLeaseExpires             :
Index                        : 7
Description                  : Intel(R) 82566MM Gigabit Network Connection
DHCPEnabled                  : False
DHCPLeaseObtained            :
DHCPServer                   :
DNSDomain                    :
DNSDomainSuffixSearchOrder   : {home}
DNSEnabledForWINSResolution  : False
DNSHostName                  : HostName
DNSServerSearchOrder         :
DomainDNSRegistrationEnabled : False
FullDNSRegistrationEnabled   : True
IPAddress                    : {192.168.168.175, 192.168.1.155, 10.10.12.89, fe80::c994:2b82:e28c:b772}
IPConnectionMetric           : 10
IPEnabled                    : True
IPFilterSecurityEnabled      : False
ArpAlwaysSourceRoute         :
ArpUseEtherSNAP              :
Caption                      : [00000007] Intel(R) 82566MM Gigabit Network Connection
DatabasePath                 : %SystemRoot%\System32\drivers\etc
DeadGWDetectEnabled          :
DefaultIPGateway             :
DefaultTOS                   :
DefaultTTL                   :
ForwardBufferMemory          :
GatewayCostMetric            :
IGMPLevel                    :
InterfaceIndex               : 11
IPPortSecurityEnabled        :
IPSecPermitIPProtocols       : {}
IPSecPermitTCPPorts          : {}
IPSecPermitUDPPorts          : {}
IPSubnet                     : {255.255.255.0, 255.255.255.0, 255.0.0.0, 64}
IPUseZeroBroadcast           :
KeepAliveInterval            :
KeepAliveTime                :
MACAddress                   : 00:21:86:96:99:58
MTU                          :
NumForwardPackets            :
PMTUBHDetectEnabled          :
PMTUDiscoveryEnabled         :
ServiceName                  : e1express
SettingID                    : {028AC479-84E5-4BBA-B68A-5240415A02D2}
TcpipNetbiosOptions          : 0
TcpMaxConnectRetransmissions :
TcpMaxDataRetransmissions    :
TcpNumConnections            :
TcpUseRFC1122UrgentPointer   :
TcpWindowSize                :
Scope                        : System.Management.ManagementScope
Path                         : \\HostName\root\cimv2:Win32_NetworkAdapterConfiguration.Index=7
Options                      : System.Management.ObjectGetOptions
ClassPath                    : \\HostName\root\cimv2:Win32_NetworkAdapterConfiguration
Properties                   : {ArpAlwaysSourceRoute, ArpUseEtherSNAP, Caption, DatabasePath...}
SystemProperties             : {__GENUS, __CLASS, __SUPERCLASS, __DYNASTY...}
Qualifiers                   : {dynamic, Locale, provider, UUID}
Site                         :
Container                    :

As you can see, this command returns detailed information about DHCP, DNS, routing, and other minor IP configuration properties.

You might also want to try…

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "DHCPEnabled=true" –ComputerName . | Get-Member

this command will give you a list of all members (Properties, Methods, etc) of the Network Adapter.

and this one…

Get-WmiObject Win32_NetworkAdapterConfiguration -computername . | select name, DNSServerSearchOrder

This will give you a list of DNS Servers

WAIT!!! Before you go any further… Please make sure you do this on a TEST Machine and not on a production machine.

Running the following commands WILL change your IP Settings and could mess up your network connections.  The way it might mess up your connections is it changes values and your network is possibly no longer seen as a domain or work network but instead a Public network… Think, no more file shares.  You have been warned!    If you want to manually change your settings back to DHCP you can do it from Network and Sharing CenterChange Adapter Settings or use the Set-IPDynamic.ps1 script below to set it back to DHCP.

Now that we know what all the properties are, we can select them for display or even change them.  Let’s see what it would look like.  This time, we want to record the information in a variable and loop through the adapters that have DHCP Enabled and set the values to new values.  If you copy the following into a Text File with a .ps1 extension and call it from the PowerShell command prompt it will loop through your NICs and change the Dynamic IPAddress  to static for all NICs that have an IP Address assigned by DHCP and the NIC is enabled.  The new IP Address it will use will be the value that was originally provided by DHCP.  In my case, I called the file Set-IPStatic.ps1.  If you have not configured your computer to run PowerShell scripts, you will need to follow the instructions in:  How To Create Enable Permissions and Run a Multi-Line PowerShell Script.

Set-IPStatic.ps1

$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -computername . | where{$_.IPEnabled -eq $true -and $_.DHCPEnabled -eq $true}
Foreach($NIC in $NICs) {
$ip = ($NIC.IPAddress[0])
$gateway = $NIC.DefaultIPGateway
$subnet = $NIC.IPSubnet[0]
$dns = $NIC.DNSServerSearchOrder
$NIC.EnableStatic($ip, $subnet)
$NIC.SetGateways($gateway)
$NIC.SetDNSServerSearchOrder($dns)
$NIC.SetDynamicDNSRegistration("FALSE")
}
IPConfig /all

# Notice I added an “IPConfig /All” command to the bottom of the script to show what my static IP Configuration Looks like!

image

If you have not enabled PowerShell Scripts to run, you will want to check out How To Create Enable Permissions and Run a Multi-Line PowerShell Script

If you ran this in a window instead of in a .ps1 script, you can exit out by pressing CTRL-C

The IPConfig /all would look something like the following:

PS C:\Windows\system32> IPConfig /all

Windows IP Configuration

   Host Name . . . . . . . . . . . . : HostName
   Primary Dns Suffix  . . . . . . . :
   Node Type . . . . . . . . . . . . : Hybrid
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No
   System Quarantine State . . . . . : Not Restricted

Ethernet adapter Test:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Intel(R) 82566MM Gigabit Network Connection
   Physical Address. . . . . . . . . : 00-21-86-96-99-58
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::c994:2b82:e28c:b772%11(Preferred)
   IPv4 Address. . . . . . . . . . . : 192.168.168.175(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.168.1
   DHCPv6 IAID . . . . . . . . . . . : 234889606
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-15-25-72-36-00-21-86-96-99-58
   DNS Servers . . . . . . . . . . . : 192.168.168.4
                                                192.168.168.5
   NetBIOS over Tcpip. . . . . . . . : Enabled

 

Set-IPDynamic.PS1

The following Script will change your Enabled Network cards to be Dynamic!

$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration `
| where{$_.IPEnabled -eq “TRUE”}
Foreach($NIC in $NICs) {
$NIC.EnableDHCP()
$NIC.SetDNSServerSearchOrder()
}
IPConfig /all

# Notice that I added an “IPConfig /All” command to the bottom of the script to show me what my new IP Configuration Looks like

Closing Remarks

I now use these scripts to elegantly switch between Static and Dynamic which I have to do often while testing and debugging or changing demo environments. These scripts are written so that it would be VERY EASY to hard-code the IP or DNS settings if you wanted to. If that is the case, the DNSServers would look something like: $dns = “192.168.168.4”, “192.168.168.5”

Hope this helps.

If you found this post useful, please leave a comment. You could also reach me via Twitter www.twitter.com/itproguru