PowerShell script for toggling network configuration between dynamic and static on Windows 7

Last Updated on April 3, 2017 by admin

Changing network configuration can be a real pain if you use your computer, a notebook for example, in more than one different network where you need a static configuration instead of a dynamic. Although Windows offers one alternate configuration it is quite circumstantial to access. The following PowerShell script will change network configurations with only a few clicks. It toggles them between dynamic and static, while it uses either static values you can enter or the ones the DHCP used itself before (default) (if you haven't executed any PowerShell scripts on your computer before, you will need to change your execution policy first - follow this tutorial to do so):

Get-WmiObject win32_networkadapterconfiguration | foreach-object { if($_.dhcpenabled -eq "true") {$akku = $_}}
if ($akku) {
$wmi = Get-WmiObject win32_networkadapterconfiguration | where{$_.ipenabled -eq "true" -and $_.dhcpenabled -eq "true"}
$ip = ($wmi.IPAddress[0])
$gate = $wmi.DefaultIPGateway
$subnet = ($wmi.ipsubnet[0])
else {
$wmi = Get-WmiObject win32_networkadapterconfiguration | where {($_.DefaultIPGateway) -and !($_.dhcpenabled)}
$akku = ""

This script will use the same values the DHCP used for your static configuration - you will want to set your own IP, Gateway and Subnet though. To do that just replace the bold printed values with the appropriate IPs in double-quotes (e.g. "").

To actually execute the script, you need to paste it into a notepad document and save it as .ps1 file. It has to be run with administrative rights, so either run it from an elevated powershell or create a link to the file's context menu that runs it in elevated mode.

If you experience issues with the character set, make sure that all double-quotes in the copied text look the same. If not, rewrite the appropriate ones.

Leave a Comment