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.

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:

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

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!

Did you know that PDQ Deploy has a PowerShell step you can use to deploy your scripts?