# 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](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) action of Hexnode UEM.

 Disclaimer:The sample scripts provided below are adapted from third-party open-source sites.

 

Find DHCP-enabled adapters
--------------------------

PowerShell script to find DHCP-enabled adapters

Get-CimInstance -Class Win32\_NetworkAdapterConfiguration -Filter "DHCPEnabled=$true" 

   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.

[![Find DHCP enabled adapters by executing the PowerShell scripts based on DHCP configuration tasks](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/07/Execute-PowerShell-scripts-for-DHCP-configuration-tasks-like-finding-DHCP-enabled-Adapters.png "Execute PowerShell scripts for DHCP configuration tasks like finding DHCP enabled Adapters")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/07/Execute-PowerShell-scripts-for-DHCP-configuration-tasks-like-finding-DHCP-enabled-Adapters.png)

To retrieve only IP-enabled adapters:

PowerShell script to retrieve only IP-enabled adapters

Get-CimInstance -Class Win32\_NetworkAdapterConfiguration -Filter "IPEnabled=$true and DHCPEnabled=$true"

   1

  Get-CimInstance -Class Win32\_NetworkAdapterConfiguration -Filter "IPEnabled=$true and DHCPEnabled=$true"

   

 

  

Retrieve DHCP properties
------------------------

PowerShell script to retrieve DHCP properties

Get-CimInstance -Class Win32\_NetworkAdapterConfiguration -Filter "IPEnabled=$true and DHCPEnabled=$true" | Format-Table -Property DHCP\* 

   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.

[![Retrieving DHCP properties by executing the PowerShell scripts for various DHCP configuration tasks](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/07/Execute-PowerShell-scripts-for-DHCP-configuration-tasks-like-retrieving-DHCP-properties.png "Execute PowerShell scripts for DHCP configuration tasks like retrieving DHCP properties")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/07/Execute-PowerShell-scripts-for-DHCP-configuration-tasks-like-retrieving-DHCP-properties.png)

Enable DHCP on each network adapter
-----------------------------------

To enable DHCP on all adapters,

PowerShell script to enable DHCP on each network adapter

$rem = 'SELECT \* from Win32\_NetworkAdapterConfiguration WHERE IPEnabled=True and DHCPEnabled=False' Invoke-CimMethod -MethodName ReleaseDHCPLease -Query $rem 

   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

PowerShell script to release DHCP leases on specific adapters

$dna = 'SELECT \* from Win32\_NetworkAdapterConfiguration WHERE DHCPServer="Enter DHCP Server address"' Invoke-CimMethod -MethodName ReleaseDHCPLease -Query $dna 

   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

PowerShell script to release DHCP leases on all adapters

Invoke-CimMethod -ClassName Win32\_NetworkAdapterConfiguration -MethodName ReleaseDHCPLeaseAll 

   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

PowerShell script to renew DHCP leases on a specific adapter

$wql = 'SELECT \* from Win32\_NetworkAdapterConfiguration WHERE DHCPServer="Enter DHCP Server address"' Invoke-CimMethod -MethodName RenewDHCPLease -Query $wql 

   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

PowerShell script to renew DHCP leases on all adapters

Invoke-CimMethod -ClassName Win32\_NetworkAdapterConfiguration -MethodName RenewDHCPLeaseAll 

   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.

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.