Using PowerShell to Set Static and DHCP IP Addresses – Part 2

Today, we’re going to cover how to set static/DHCP IP (pronounced duhiccup’ip…Wrongly.) addresses via Get-WMIObject and WMI (Windows Management Instrumentation), which will work for those of us still using Windows 7.

This is part two of setting static/DHCP IP addresses. Last blog post, we used the newer cmdlets that are available in Windows 8 and later operating systems to set static/dynamic IP addresses. You should check out that post if you haven’t already.

Get-WmiObject

This is the cmdlet that allows you to access information in WMI via different classes in the WMI namespace. Here’s a good article that shows all the different classes that are available.

Those of you who are familiar may recognize these commands. We’re going to use WMI and the Win32_NetworkAdapterConfiguration class.

If you scroll a bit down, you’ll see a list of methods and properties that we can use with the Win32_NetworkAdapterConfiguration class. Additionally, you can browse what’s available directly from Powershell by using the following:

Get-WmiObject Win32_NetworkAdapterConfiguration | Get-Member

Get-WmiObject

Properties:

IPEnabled – This value shows whether TCP/IP is enabled

Methods:

EnableDHCP() – Enables DHCP
EnableStatic(IP Address, Subnet Mask) – Enables static TCP/IP addressing
SetGateways(IP Gateways, Cost metric) – Sets the IP gateway. Cost metric default value is 1
SetDNSServerSearchOrder(DNS Servers) – Set the DNS Servers

Usage Examples

We’re going to give a couple of examples for setting static and DHCP-enabled IP addresses that should work in most environments.

Note: In my examples, I had to add a sleep of 4 seconds to my Set Static IP script in order to get it to work. For some reason, setting the gateway immediately would sometimes give error 67 (return code values here) when changing the settings on one of my test virtual machines. So, you may be able to modify/remove that line altogether in your environment.

Setting an address with DHCP – Using WMI

$IP = "10.0.1.123"
$NetMask = "255.255.248.0"
$Gateway = "10.0.0.1"
$DNS = "10.0.7.203"
$adapter = Get-WmiObject win32_NetworkAdapterConfiguration `
 -filter "IPEnabled = 'true'"
$adapter.EnableStatic($IP, $NetMask)
Sleep -Seconds 4
$adapter.SetGateways($Gateway)
$adapter.SetDNSServerSearchOrder($DNS)

Setting an address with DHCP – Using WMI

$adapter = Get-WmiObject win32_NetworkAdapterConfiguration `
 -filter "IPEnabled = 'true'"
# Configure the DNS Servers automatically
$adapter.SetDNSServerSearchOrder()
# Enable DHCP
$adapter.EnableDHCP()

On my test system, I have two network adapters – one is active and one is inactive. Similar to the last blog post, we’re going to set the $adapter variable in both examples to be the active network adapter. You will need to adjust this to suit your environment.

Add the $DNS values in the order you wish the DNS servers to be used. When we don’t wish to provide any values to SetDNSServerSearchOrder() it will set it back to obtain the DNS servers automatically.

Final Thoughts

This is still just scratching the surface of what’s available. You can use it to set your DNS suffixes, WINS settings, etc. Remember to check out what properties and methods are available for Win32_NetworkAdapterConfiguration and you’ll be able to customize all the settings for your particular environment.

Best of luck to you!