Powershell script to change from static to dynamic IP addressing

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.

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.

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(“”, “”)
  $DNSServers = “”,”″

### set dynamic addressing – save as setdynamic.ps1
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration `
| where{$_.IPEnabled -eq “TRUE”}
Foreach($NIC in $NICs) {

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.

Comments [3]

  1. I use the following batch files to accomplish this…
    To switch to dhcp
    netsh interface ip set address “Local Area Connection” dhcp
    netsh interface ip set dns “Local Area Connection” dhcp

    To assign an ip address
    netsh interface ip set address name=”Local Area Connection” static 1
    netsh interface ip set dns “Local Area Connection” static

    For static the first set is your ip, then subnet mask then gateway then metric. I use as it’s a public dns server and easy to remember.

  2. Hi, Julie
    I have both IPv4 and IPv6 on my machine,
    after I ran setdynamic.ps1, IPv6 is still static.
    then how to apply dynamic IP address on IPv6 using Powershell?

  3. Hi Julie,
    I have found this script very useful, but have one question, how would you set it to run the same type of info on remote machines, that I have listed in a txt file. For example, I have recently decommissioned a dc and need to change the dns and wins listings on about 50 servers and would like to script out the process rather than login and perform manually.


Leave a Reply

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