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.

Disclaimer:

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

Find DHCP-enabled adapters

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

To retrieve only IP-enabled adapters:

Retrieve DHCP properties

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

Enable DHCP on each network adapter

To enable DHCP on all adapters,

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

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

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

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

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.

  • Sample Script Repository