Renew Dynamic IP Addresses in WSH

“Is there any way to release and renew a dynamic IP address in WSH?” – Chris. F

Releasing and renewing dynamic IP addresses can be a very effective method of repairing a broken internet connection. Effectively, you are instructing your network adapter to request a new IP address from a DHCP server. While the Windows Script Host doesn’t provide any method of doing this directly, the Windows Management Instrumentation, or WMI, does.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" _
		& strComputer & "\root\cimv2")
Set colNetCards = objWMIService.ExecQuery _
	("Select * From Win32_NetworkAdapterConfiguration" _
		& " Where IPEnabled = True")

You’ll need to start by connecting to the local WMI Service. Then you’ll need to execute a query that returns a collection of network adapter configurations. Notice that this query uses a Where clause to limit the results that are returned. By specifying that the IPEnabled property be set to True, we ensure that we’re only dealing will those network adapters that are set to use DHCP IP addresses.

For Each objNetCard In colNetCards

Releasing and renewing IP addresses is now as simple as moving through the collection making calls to the ReleaseDHCPLease and RenewDHCPLease methods provided by the Network Adapter Configuration objects.


Like the read? Share it!

1 Comment

Leave a Reply


Wanna say hello?
Drop us a line!

You'll find us here

1 Microsoft Way,
WA 98052, United States