PowerShell Function Set-IPAddress

I recently came across the need to configure IP information on a Network interface using a script. This is definitely something that can be accomplished with NETSH,  but I was looking for a way to do it with native PowerShell using Get-WmiObject. If there is a choice, I tend to prefer to stick with native Powershell Cmdlets and functions rather than running cmd based tools, but that is just my personal opinion and you can take it or leave it.

My goal was to be able to specify a Network Interface by name, ie “Local Area Connection” and pass it an IP address, mask, gateway, and 1 or 2 DNS servers. In order to accomplish this there were two WMI classes that I needed. The first is Win32_NetworkAdapter. The second is Win32_NetworkAdapterConfiguration. The real key (pun intended) was to figure out a way to join the information from these two classes so that I knew for sure that I was working with the same network interface. It turns out that both classes have a property called InterfaceIndex.

Win32_NetworkAdapter has a property called netconnectionid which is equal to the name of the Adapter, ie “Local Area Connection”.

With that information, here is a function that you can use to specify settings for a NIC based on the name of the NIC.

   1: function Set-IPAddress {
   2:         param(  [string]$networkinterface,
   3:                 [string]$ip,
   4:                 [string]$mask,
   5:                 [string]$gateway,
   6:                 [string]$dns1,
   7:                 [string]$dns2,
   8:                 [string]$registerDns = "TRUE"
   9:          )
  12:         #Start writing code here
  13:         $dns = $dns1
  14:         if($dns2){$dns ="$dns1,$dns2"}
  15:         $index = (gwmi Win32_NetworkAdapter | where {$_.netconnectionid -eq $networkinterface}).InterfaceIndex
  16:         $NetInterface = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.InterfaceIndex -eq $index}
  17:         $NetInterface.EnableStatic($ip, $mask)
  18:         $NetInterface.SetGateways($gateway)
  19:         $NetInterface.SetDNSServerSearchOrder($dns)
  20:         $NetInterface.SetDynamicDNSRegistration($registerDns)
  22: }

Hope you find this useful.



5 Responses to “PowerShell Function Set-IPAddress”

  1. Nola Says:

    Very nice post. I just stumbled upon your weblog and wanted
    to say that I’ve really enjoyed surfing around your blog posts. In any case I’ll be subscribing to your feed and I
    hope you write again soon!

  2. Juanita Says:

    Hey would you mind stating which blog platform you’re working with? I’m looking to
    start my own blog soon but I’m having a tough time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I’m looking for something completely unique.

    P.S Sorry for getting off-topic but I had to ask!

  3. Hide My Ass: a Professional IP Changer Program - Replace Your Proxies Now Says:

    Heya! I know this is sort of off-topic but I had to ask.
    Does running a well-established blog such as yours require
    a large amount of work? I’m completely new to writing a blog but I do write in my diary daily. I’d like to start a blog so I
    can share my experience and thoughts online. Please let
    me know if you have any ideas or tips for brand new aspiring bloggers.
    Appreciate it!

  4. Gerry Pearson Says:

    Thanks! Just what I was looking for. One comment though. The SetDNSServerSearchOrder method takes either a string or an array of strings. I think the line:
    if($dns2){$dns =”$dns1,$dns2″}

    should actually be more like this:
    if($dns2){$dns =@($dns1,$dns2)}

  5. home depot promotional codes Says:

    Hi! I’ve been reading your website for a while now and finally got
    the bravery to go ahead and give you a shout out
    from Houston Texas! Just wanted to mention keep up the great job!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: