I’ve been working on validating an email server migration plan which has required utilizing test servers isolated from the production network.  Because they are offnet, I have to frequently switch my laptop from the production to test networks, which involves reconfiguring my network card from dynamic to static IP addresses.

I could use Windows XP’s alternate IP addressing capability to change IP addresses, but I find it takes several minutes for XP to make the automatic changeover.  I wanted to create a configuration script that would make the desired changes so that I could save shortcuts to those scripts on my desktop, which would allow for quick NIC setting modifications.

Table of Contents

    I originally tried to use the netsh command to specify NIC settings, but despite all the example syntax I found online, I was only able to get the static to dynamic changes to work, not the dynamic to static.

    Powershell Script to Change from Static to Dynamic IP Addressing image 1

    I finally decided to use Powershell to make these changes.  I found the following scripts at PowerShell Pro and modified them ever so slightly for my own use.

    ### set static IP addressing - save as setstatic.ps1
     $NICs = Get-WMIObject Win32_NetworkAdapterConfiguration `
     | where{$_.IPEnabled -eq "TRUE"}
     Foreach($NIC in $NICs) {
       $NIC.EnableStatic("192.168.1.5", "255.255.255.0")
       $NIC.SetGateways("192.168.1.254")
       $DNSServers = "198.168.1.1","198.168.1.1"
       $NIC.SetDNSServerSearchOrder($DNSServers)
       $NIC.SetDynamicDNSRegistration("FALSE")
     }
     ###
    ### set dynamic addressing - save as setdynamic.ps1
     $NICs = Get-WMIObject Win32_NetworkAdapterConfiguration `
     | where{$_.IPEnabled -eq "TRUE"}
     Foreach($NIC in $NICs) {
      $NIC.EnableDHCP()
      $NIC.SetDNSServerSearchOrder()
     }
     ###

    I then created shortcuts to the two .ps1 files using the following targets:

    %windir%\system32\WindowsPowerShell\v1.0\powershell.exe c:\scripts\setdynamic.ps1
    
    %windir%\system32\WindowsPowerShell\v1.0\powershell.exe c:\scripts\setstatic.ps1

    I saved each shortcut to my desktop, and now I can switch IP addresses in about 15 seconds.

    Leave a Reply

    Your email address will not be published. Required fields are marked *