Category filter
PowerShell scripts for DHCP configuration tasks
In the realm of network management, DHCP (Dynamic Host Configuration Protocol) serves as a foundational service that automates the assignment of IP addresses and network configurations to devices, ensuring seamless connectivity within the network. For IT admins seeking to streamline and automate DHCP configuration tasks, PowerShell scripts emerge as a powerful and efficient solution. To remotely deploy these PowerShell scripts for DHCP configuration tasks, IT admins can use the Execute Custom Script action of Hexnode UEM.
Find DHCP-enabled adapters
1 |
Get-CimInstance -Class Win32_NetworkAdapterConfiguration -Filter "DHCPEnabled=$true" |
The Get-CimInstance
cmdlet retrieves information from Win32_NetworkAdapterConfiguration class, selecting only the DHCP-enabled adapters using the Filter
parameter.
To retrieve only IP-enabled adapters:
1 |
Get-CimInstance -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=$true and DHCPEnabled=$true" |
Retrieve DHCP properties
1 |
Get-CimInstance -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=$true and DHCPEnabled=$true" | Format-Table -Property DHCP* |
Using the Property
parameter of Format-Table
, the script displays only the DHCP-related properties of IP-enabled and DHCP-enabled network adapters.
Enable DHCP on each network adapter
To enable DHCP on all adapters,
1 2 3 |
$rem = 'SELECT * from Win32_NetworkAdapterConfiguration WHERE IPEnabled=True and DHCPEnabled=False' Invoke-CimMethod -MethodName ReleaseDHCPLease -Query $rem |
The filter statement IPEnabled=True and DHCPEnabled=False makes sure the script avoids enabling DHCP for already DHCP-enabled adapters.
Release DHCP lease
Release DHCP leases on specific adapters
1 2 3 |
$dna = 'SELECT * from Win32_NetworkAdapterConfiguration WHERE DHCPServer="Enter DHCP Server address"' Invoke-CimMethod -MethodName ReleaseDHCPLease -Query $dna |
To release DHCP leases for a specific network adapter using the Invoke-CimMethod
cmdlet, you can call the ReleaseDHCPLease method. However, you will need to specify the DHCP server address of the desired network adapter using a filter.
For example, the following command releases all DHCP leases on adapters on the local computer that are obtaining DHCP leases from 192.168.1.254:
$dna = 'SELECT * from Win32_NetworkAdapterConfiguration WHERE DHCPServer="192.168.1.1"'
Invoke-CimMethod -MethodName ReleaseDHCPLease -Query $dna
Release DHCP leases on all adapters
1 |
Invoke-CimMethod -ClassName Win32_NetworkAdapterConfiguration -MethodName ReleaseDHCPLeaseAll |
To release DHCP leases on all adapters using Invoke-CimMethod
, you can utilize the Win32_NetworkAdapterConfiguration WMI class and call the ReleaseDHCPLeaseAll
method.
Renew DHCP lease
Renew DHCP leases on a specific adapter
1 2 3 |
$wql = 'SELECT * from Win32_NetworkAdapterConfiguration WHERE DHCPServer="Enter DHCP Server address"' Invoke-CimMethod -MethodName RenewDHCPLease -Query $wql |
For renewing the DHCP lease on a specific adapter, call the RenewDHCPLease method for the Invoke-CimMethod
cmdlet, specifying the adapter using the DHCP server address.
E.g., $dna = 'SELECT * from Win32_NetworkAdapterConfiguration WHERE DHCPServer="192.168.1.1"'
Invoke-CimMethod -MethodName ReleaseDHCPLease -Query $dna
renews all DHCP leases on adapters on the local computer that are obtaining DHCP leases from 192.168.1.254.
Renew DHCP leases on all adapters
1 |
Invoke-CimMethod -ClassName Win32_NetworkAdapterConfiguration -MethodName RenewDHCPLeaseAll |
To release DHCP leases on all adapters using Invoke-CimMethod
, you can utilize the Win32_NetworkAdapterConfiguration WMI class and call the RenewDHCPLeaseAll
method.