Skip to content

Greg Altman

Solutions, Learnings, PowerShell, and random thoughts.

Menu
  • Home
  • Tech Reviews
  • Github Repos
  • About Me
Menu

Convert DHCP Lease to Reservation with Powershell

Posted on June 10, 2018 by Greg Altman

Scenario :  You have just created (by whatever means) a new server. It has gotten an IP address from DHCP and all is working as it should…..However…. Perhaps this server provides a service that requires a static IP (for whatever reason).

In the old days, you would fire up the DHCP tool from the RSAT tools or <shudder> RDP into your domain controller to convert the dynamic DHCP Lease into a reservation. There is a faster way…… if you guessed Powershell – you are correct 🙂

It took a little spelunking around to work this out but here are the steps to follow.

First, create a remote powershell session to your DHCP server.

[code lang=’powershell’] Enter-PSSession -ComputerName MyDC.houndtech.pri [/code]

Next find out the IP address  of the new server. Easy enough with :

[code lang=’powershell’]Test-NetConnection -computername Newserver.houndtech.pri [/code]

Let’s make things a little easier for later and put that returned object in a variable.

[code lang=’powershell’]$x = Test-NetConnection -computername Newserver.houndtech.pri [/code]
But we don’t need the whole object, just the IP address, so let’s narrow it down with

[code lang=’powershell’]$IP = $x.BasicNameResolution.IPaddress [/code]

Now $IP contains JUST the IP address, which is what we need for the next series of cmdlets.
Next we retrieve the DHCP lease object with

[code lang=’powershell’] Get-DHCPServerV4Lease -IPAddress $IP [/code]

Finally pipe it to the cmdlet to add the reservation.

[code lang=’powershell’] Get-DHCPServerV4Lease -IPAddress $IP | Add-DHCPServerV4Reservation [/code]

Of course this could be piped together into a sweet oneliner that would go well added to any automated provisioning script.

[code lang=’powershell’] Get-DHCPServerV4Lease -IPAddress ((Test-NetConnection -ComputerName ‘NewServer.HoundTech.pri’).RemoteAddress.IPAddressToString)| Add-DhcpServerv4Reservation [/code]

See you next time!

Share this:

  • Click to share on Bluesky (Opens in new window) Bluesky
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

Disclaimer: The thoughts, ideas and code on this blog are my own and do not represent any employer, past or present. All code is for demonstration and educational purposes only. Use at your own risk

Categories

  • Ad-hoc and Random
  • Blog
  • Career
  • Cloud
  • Events
  • PowerShell
  • Reviews
  • Security
  • Server Stuff
  • Troubleshooting
  • Uncategorized

Overall Rating
0.0
© 2026 Greg Altman | Powered by Minimalist Blog WordPress Theme
%d