Managing DHCP from PowerShell

This article represents using PowerShell for managing network settings and managing DHCP.

You can change network settings using Win32_NetworkAdapterConfiguration. The Win32_NetworkAdapterConfiguration WMI class represents the attributes and behaviors of a network adapter. This class includes properties and methods that support the management of the TCP/IP and IPX protocols.

Win32_NetworkAdapterConfiguration methods and properties

Through the properties and methods provided by the WMI class Win32_NetworkAdapterConfiguration, you can enumerate DHCP settings, enable or disable DHCP, and renew or release releases.

Win32_NetworkAdapterConfiguration IP Address Allocation Methods

EnableDHCP – Enables the Dynamic Host Configuration Protocol (DHCP) for service with this network adapter.

EnableStatic – Enables static TCP/IP addressing for the target network adapter.

ReleaseDHCPLease - Releases the IP address bound to a specific DHCP enabled network adapter.

ReleaseDHCPLeaseAll - Releases the IP addresses bound to all DHCP enabled network adapters.

RenewDHCPLease - Renews the IP address on specific DHCP-enabled network adapters.

RenewDHCPLeaseAll - Renews the IP addresses on all DHCP-enabled network adapters.

SetGateways - Specifies a list of gateways for routing packets destined for a different subnet than the one this adapter is connected to.

Win32_NetworkAdapterConfiguration IP Address Allocation Properties

DefaultIPGateway – Array of IP addresses of default gateways that the computer system uses. Example: “192.168.12.1 192.168.46.1″

DHCPEnabled – If TRUE, the dynamic host configuration protocol (DHCP) server automatically assigns an IP address to the computer system when establishing a network connection.

DHCPLeaseExpires – Expiration date and time for a leased IP address that was assigned to the computer by the dynamic host configuration protocol (DHCP) server.

DHCPLeaseObtained – Date and time the lease was obtained for the IP address assigned to the computer by the dynamic host configuration protocol (DHCP) server.

DHCPServer – IP address of the dynamic host configuration protocol (DHCP) server.

GatewayCostMetric – Array of integer cost metric values (ranging from 1 to 9999) to be used in calculating the fastest, most reliable, or least expensive routes. This argument has a one-to-one correspondence with the DefaultIPGateway property.

IPAddress – Array of all of the IP addresses associated with the current network adapter. Starting with Windows Vista, this property can contain either IPv6 addresses or IPv4 addresses. For more information, see IPv6 and IPv4 Support in WMI. Example IPv6 address: “2010:836B:4179::836B:4179″

Windows Server 2003, Windows XP, Windows 2000, and Windows NT 4.0: This property contains addresses in IPv4 format. Example: “172.16.22.0″

IPConnectionMetric – Cost of using the configured routes for the IP bound adapter and is the weighted value for those routes in the IP routing table. If there are multiple routes to a destination in the IP routing table, the route with the lowest metric is used. The default value is 1. Windows 2000 and Windows NT 4.0: This property is not available.

IPEnabled – If TRUE, TCP/IP is bound and enabled on this network adapter.

IPSubnet – Array of all the subnet masks associated with the current network adapter.

Managing DHCP from Powershell

You can find the DHCP-enabled adapters on a computer by using this command:

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "DHCPEnabled=true" -ComputerName .

DHCP-enabled adapters with TCP/IP:

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=true and DHCPEnabled=true" -ComputerName .

Enabling DHCP

Enable DHCP on all network adapters:

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=true -ComputerName . | ForEach-Object -Process {$_.InvokeMethod("EnableDHCP", $null)}

Releasing DHCP Leases

Releasing a DHCP lease for a network adapter disconnects that adapter from the network and releases the IP address. The ipconfig.exe /release command performs this task on all network adapters or on a specified adapter. For example, the following command will release all DHCP leases on adapters that are obtaining DHCP leases from 192.168.1.1:

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=true and DHCPEnabled=true" -ComputerName . | Where-Object -FilterScript {$_.DHCPServer -contains "192.168.1.1"} | ForEach-Object -Process {$_.InvokeMethod("ReleaseDHCPLease",$null)}

Renewing DHCP Leases

DHCP also allows leases to be manually or programmatically renewed on the client, bypassing the normal renegotiation process between client and server. The ipconfig.exe /renew command performs this task for all network adapters or a specified adapter. For example, the following command will release all DHCP leases on adapters that are obtaining DHCP leases from 192.168.1.1:

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=true and DHCPEnabled=true" -ComputerName . | Where-Object -FilterScript {$_.DHCPServer -contains "192.168.1.254"} | ForEach-Object -Process {$_.InvokeMethod("ReleaseDHCPLease",$null)}

Releasing and Renewing DHCP Leases on All Adapters

You can perform global DHCP address releases or renewals on all adapters by using the Win32_NetworkAdapterConfiguration methods, ReleaseDHCPLeaseAll and RenewDHCPLeaseAll.

Using ReleaseDHCPLeaseAll method:

( Get-WmiObject -List | Where-Object -FilterScript {$_.Name -eq "Win32_NetworkAdapterConfiguration"} ) .InvokeMethod("ReleaseDHCPLeaseAll", $null)

Using RenewDHCPLeaseAll method:

( Get-WmiObject -List | Where-Object -FilterScript {$_.Name -eq "Win32_NetworkAdapterConfiguration"} ) .InvokeMethod("RenewDHCPLeaseAll", $null)

Jul 25, 2017

Similar articles