Windows Script to Change Network Settings

Automating Network Configuration with VB Script
Recently, a recurring challenge presented itself. Many professional settings necessitate adjustments to network configurations when transitioning between different networks.
Consider individuals who utilize laptops both at the office and at home. A static IP address might be configured for home use, while the workplace typically employs DHCP. This demands frequent manual alterations to network settings.
The Problem of Frequent Network Switching
This scenario is particularly prevalent among engineers and IT professionals who routinely connect to isolated networks within a building. Accessing these networks often requires switching to a static IP configuration.
Subsequently, reconnecting to the corporate network necessitates reverting to DHCP. This constant toggling can be time-consuming and frustrating.
A Solution: VB Script Automation
Having encountered this issue repeatedly, and growing weary of navigating network card settings manually, a decision was made to develop a VB script to streamline the process.
As those familiar with previous programming articles know, VB scripts are a favored tool. They have been previously utilized to create a simulated "possessed" computer and to automate data backups using Microsoft SyncToy.
Script Flexibility and User Input
Automating this task with a VB script is not only feasible but can also be designed to accept user-defined static IP addresses, enhancing its adaptability.
This article will detail the implementation of this solution, broken down into three distinct sections.
- The first section will cover the foundational elements of the script.
- The second will focus on incorporating user input for the static IP.
- Finally, the third section will demonstrate the complete, functional script.
Creating a Network Setting Change Script
To develop a utility for switching network configurations, three primary objectives must be met through scripting. Initially, the script should facilitate the creation of static IP settings. Subsequently, a script to activate DHCP is required. Lastly, the script needs to prompt the user for their desired action and execute it accordingly.
VB Script for Configuring Static IP Settings
It’s important to remember that these scripts must be saved as a text file with a .wsf extension to function correctly on a Windows operating system. The following script modifies network settings to utilize a static IP address, complete with a specified subnet mask and default gateway – all three values are directly embedded within the script itself.
For all code examples presented in this article, ensure the inclusion of "<job><script language="VBScript">" at the beginning and "</script></job>" at the end; otherwise, the code will not execute.
Here is the script designed to implement the static IP change:
Option ExplicitOn Error Resume Next
Dim objWMIService
Dim objNetAdapter
Dim strComputer
Dim arrIPAddress
Dim arrSubnetMask
Dim arrGateway
Dim colNetAdapters
Dim errEnableStatic
Dim errGateways
strComputer = "."
arrIPAddress = Array("192.168.1.106")
arrSubnetMask = Array("255.255.255.0")
arrGateway = Array("192.168.1.1")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each objNetAdapter in colNetAdapters
errEnableStatic = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask)
If Not errEnableStatic = 0 Then
WScript.Echo "Failure assigning IP/Subnet."
End If
errGateways = objNetAdapter.SetGateways(arrGateway)
If Not errGateways = 0 Then
WScript.Echo "Failure assigning Gateway."
End If
Next
WScript.Quit
This script leverages the Windows WMI service to modify network settings. The script utilizes three predefined array variables containing the IP address, subnet mask, and gateway information. It then identifies the active, enabled network adapter and employs the "EnableStatic" and "SetGateways" methods to implement the necessary changes. Upon execution on a network configured for DHCP, the script successfully altered adapter settings, resulting in a loss of internet connectivity.

Having validated the functionality of the static IP portion of the script, the next step involves creating a script to configure the adapter to use DHCP, enabling automatic network IP detection. The following script accomplishes this.
Option ExplicitOn Error Resume Next
Dim objWMIService
Dim objNetAdapter
Dim strComputer
Dim errEnable
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableDHCP()
Next
WScript.Quit
This script is considerably simpler than the previous one. It also utilizes WMI, but only requires the "EnableDHCP" function. This function is applied to the currently enabled network adapter. Following execution, the adapter settings reverted to DHCP, restoring internet connectivity.

With both essential actions now scripted, the next phase involves incorporating user input to specify the desired static IP address. To activate DHCP, the user can input the word "AUTO" for automatic IP detection.
Here's the complete script, integrating both previous scripts and user input:
Option ExplicitOn Error Resume Next
Dim objWMIService
Dim objNetAdapter
Dim strComputer
Dim arrIPAddress
Dim arrSubnetMask
Dim arrGateway
Dim colNetAdapters
Dim errEnableStatic
Dim errGateways
Dim strInput
Dim errFailed
errFailed = 0
strInput = InputBox("Type Static IP Address or AUTO")
If strInput = "AUTO" Then
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableDHCP()
If Not errEnable = 0 Then
WScript.Echo "Setting DHCP Failed."
errFailed = 1
End If
Next
Else
strComputer = "."
arrIPAddress = Array(strInput)
arrSubnetMask = Array("255.255.255.0")
arrGateway = Array("192.168.1.1")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each objNetAdapter in colNetAdapters
errEnableStatic = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask)
If Not errEnableStatic = 0 Then
WScript.Echo "Failure assigning IP/Subnet."
errFailed = 1
End If
errGateways = objNetAdapter.SetGateways(arrGateway)
If Not errGateways = 0 Then
WScript.Echo "Failure assigning Gateway."
errFailed = 1
End If
Next
End If
If errFailed = 0 Then
WScript.Echo "IP Settings Successfully Modified."
End If
WScript.Quit
This script utilizes the InputBox function to obtain either the static IP address or the "AUTO" command from the user.
If the user enters anything other than "AUTO", that input is used as the static IP address within the WMI-based code that configures the network adapter. A final check confirms successful operation.

After running the script, network adapter settings were verified to reflect the specified static IP configuration.

To test the "AUTO" functionality, entering "AUTO" triggers the corresponding section of the "IF" statement, initiating the DHCP enabling script.

Following execution, adapter settings were confirmed to be set to "Obtain an IP address automatically".

While manually adjusting network card settings between static and DHCP is straightforward, frequent changes can become cumbersome. Using the command line with "netsh" is an alternative, but requires memorizing the command syntax.
This script provides a convenient and easily accessible utility for quickly switching network settings as needed.
Are you considering implementing this Windows Script? Do you have any suggestions for further enhancements? Share your thoughts and ideas in the comments below.
Image Credit:Binary Codes via Shutterstock
Related Posts

Touchscreen on Windows PC: Do You Need It?

Find Lost Windows or Office Product Keys - Easy Guide

Windows 10 Resetting Settings: Why It Happens & How to Fix

Monitor FPS in UWP Games on Windows 10 - A Simple Guide
Remove 'Get Windows 10' Icon & Stop Upgrade Notifications
