Configure static or dynamic IP and DNS with Powershell

I work in a lot of different offices and conference venues, in addition to my home office. At home, I need a static DNS server address; everywhere else, it has to be dynamic. Tired of the click-click-clickety-click it takes to set, unset, and reset my network adapter settings, I went looking for a Powershell solution. This is the kind of problem that Powershell is made for, right? Right!

We need the Get-WmiObject cmdlet*, along with the win32_NetworkAdapterConfiguration class.

What’s my IP?

For starters, let’s use Get-WmiObject and that very long class name to get our network adapter settings:

$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";

If we glance over what methods and properties are available in the new object ($wmi | get-member;) the IPAddress property leaps out at me. We can use that to display our current IP address**:

$wmi.IPAddress;

That was easy. You can also get your:

  • Subnet mask: $wmi.IPSubnet
  • DNS server address: $wmi.DNSServerSearchOrder
  • Gateway: $wmi.DefaultIPGateway

Set Static IP, Gateway, DNS server

Here’s where we note that we
SHOULD NOT DO THIS ON A PRODUCTION SYSTEM.
You’re messing with the network settings, remember. 

DNS_staticLet’s go ahead and set a static IP, gateway, and DNS server, using that same $wmi object from above. The script would then look like this:

$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";
$wmi.EnableStatic("IP address", "Subnet mask");
$wmi.SetGateways("Gateway", 1);
$wmi.SetDNSServerSearchOrder("DNS server address");

EnableStatic allows you to set a specific IP address and subnet mask. SetGateways is for the default gateway. And SetDNSServerSearchOrder allows you to set static preferred and/or alternate DNS servers.

As an example:

$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";
$wmi.EnableStatic("10.0.0.15", "255.255.255.0");
$wmi.SetGateways("10.0.0.1", 1);
$wmi.SetDNSServerSearchOrder("127.1.1.12");

This example results in the settings you see in the image on the right: all static, all the time.

Set Dynamic IP, Gateway, DNS server

DNS_dynamicWhen I’m not at home, everything must be dynamic. It took a bit of digging – not a lot of folks are blogging about setting dynamic network adapter settings – but it turns out to be easy, of course!

$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";
$wmi.EnableDHCP();
$wmi.SetDNSServerSearchOrder();

DHCP “allows IP addresses to be dynamically allocated”, and running SetDNSServerSearchOrder*** with no parameters (or alternately, with $null) sets the network adapter to get a DNS server address dynamically. Brilliant!

That’s all there is to it, and you can feel free to stop reading here. Unless, of course, you want to get into the muddle that is Get-CimInstance in Powershell 3.0…

But what about CIM?

Some of you may be shouting about Powershell 3.0 and Get-CimInstance; let me say for now that I had no end of trouble with it, I’m still working on it, and there will be a future blog post. And yes, I did find that chapter in the Windows Powershell Cookbook that spells out the use of CIM for this, and yes, I had trouble with that as well. All I can say is, I’m still working on it, and WMI seems to work just fine, thankyouverymuch.

Happy days,
Jen McCown
MidnightDBA.com/Jen

 

* WMI stands for “Windows Management Instrumentation“…Windows administrative settings and general stuff.

** By the way, you could get also use test-connection computername, a ping equivalent, to get your IP. But today we’re talking Get-CimInstance.

*** On setting DNS to dynamic: In my searching, I found this solution, but have had zero luck with it effecting the change: SetDynamicDNSRegistration(“TRUE”)

6 thoughts on “Configure static or dynamic IP and DNS with Powershell

  1. Pingback: Dew Drop – March 5, 2014 (#1735) | Morning Dew

  2. Shawn Melton

    Excellent stuff… I used to use something like this to actually send me my current IP address by email so I could remote into it when away from my desk; was before I had a laptop.

  3. Frank James

    I am using this to set the IP

    $nic = Get-WmiObject Win32_NetworkAdapterConfiguration -filter “ipenabled = ‘true'”
    $ip_address = “10.2.51.44”
    $subnetMask = “255.255.255.224”
    $gateway = “10.2.34.65”
    $nic.EnableStatic($ip_address,$subnetmask)
    $nic.SetGateways($gateway,1)
    $dns = “10.2.51.23”,”10.2.51.24″
    $nic.SetDNSServerSearchOrder($dns)

    What I am having issue with is when the Network is disconnected the Script does not work and errors out, only if the Cable is plugged in, I know it has to do with how its being called but can’t seem to get the correct syntax, any ideads?

  4. www.bluecleanandclear.com

    Hello, i think that i saw you visited myy sikte thgus i ccame to “return tthe
    favor”.I am trying to find things to enhance myy website!I suppose its
    ok to use some of your ideas!!

Comments are closed.