# Get Free Ip Address in a DHCP server in reservation mode

**URL:** https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701
**Category:** PowerShell Help
**Created:** [August 2, 2023, 1:44pm UTC](https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701 "2023-08-02T13:44:08Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Sokariz](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Sokariz](https://forums.powershell.org/u/Sokariz)
#### Post date: [August 2, 2023, 1:44pm UTC](https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701/1 "2023-08-02T13:44:08Z")

</div>

Good morning everyone,  
First of all I’d like to apologize for my English as I’m French.  
I am a beginner in powershell in an internship as a technician where my administrator asked me to get all non used Ip adresses. The goal is for them to be able to easily find which Ip address to give to someone new in the company.  
I cannot use the cmdlet Get-DhcpServer4FreeIpAdress as it excludes all reserved addresses.  
So my plan was to use Get-DhcpServer4Lease then to use an operation in which I will take each Ip address and add +1, whenever the Ip address isn’t match in the list it would be displayed.  
So far I tried with where-object after a pipe but it wasn’t conclusive at all… if you could help me?  
Thank you !!

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [August 2, 2023, 2:14pm UTC](https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701/2 "2023-08-02T14:14:38Z")

</div>

Math,  
Welcome to the forum. 👋🏼

> [@Sokariz](#):
>
> First of all I’d like to apologize for my English as I’m French.

Nobody is perfect. 😉

> [@Sokariz](#):
>
> I cannot use the cmdlet Get-DhcpServer4FreeIpAdress as it excludes all reserved addresses.

Hmmm … why not using `Get-DhcpServer4FreeIpAdress` AND `Get-DhcpServerv4Reservation` and comapre them with `Compare-Object`?

Regardless of that …

> [@Sokariz](#):
>
> So far I tried with where-object after a pipe but it wasn’t conclusive at all… if you could help me?

Please do describe your code - post it! 😉

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( \</\> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

[How to format code in PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) 1 \<---- Click 👆🏽 😉

( !! Sometimes the _preformatted text_ button hides behind the settings gear symbol. 😉 )

---

<div class="post-metadata">

### Author: ![Sokariz](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Sokariz](https://forums.powershell.org/u/Sokariz)
#### Post date: [August 2, 2023, 2:35pm UTC](https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701/3 "2023-08-02T14:35:33Z")

</div>

```auto
< $tableau = @()
$ipa = Get-DhcpServerv4Lease -ComputerName "dhcpservername" -ScopeId 192.168.0.0(just an exemple) | Select-Object IPAddress
function IncrementLastOctet($ipAddress) {
$octets = $ipAddress -split '\.'
    $lastOctet = [int]$octets[-1]
    $newLastOctet = ($lastOctet + 1) 
    $octets[-1] = $newLastOctet
        return $octets -join '.'
}
foreach($ip in $ipa){
    $NouvelleAddresseIP = IncrementLastOctet $ip 
    if ($tableau -notcontains $NouvelleAddresseIP) {
        $tableau += $NouvelleAddresseIP  
    }
}>

```

I cannot use Get-DhcpServerv4FreeIpAddress as the server is set on ip reservation and there’s an exclusion range that cover the entire network. For security reasons they have practically removed the main purpose of a dhcp server. So when I try that cmdglet I get

---

<div class="post-metadata">

### Author: ![Sokariz](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Sokariz](https://forums.powershell.org/u/Sokariz)
#### Post date: [August 2, 2023, 2:36pm UTC](https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701/4 "2023-08-02T14:36:36Z")

</div>

I get a warning saying that it cannot find free IP addresses. But when I use Get-DhcpServerv4Lease I can see that Ip addresses are missing.

---

<div class="post-metadata">

### Author: ![Sokariz](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Sokariz](https://forums.powershell.org/u/Sokariz)
#### Post date: [August 2, 2023, 2:40pm UTC](https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701/5 "2023-08-02T14:40:00Z")

</div>

In my code I tried to focus on the last octet to add +1 and whenever the ip address that’s supposed to be found isn’t, it writes it in the board ( $tableau). But so far it hasn’t worked out.

---

<div class="post-metadata">

### Author: ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)
#### Post date: [August 2, 2023, 6:26pm UTC](https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701/6 "2023-08-02T18:26:23Z")

</div>

“Hasn’t worked out” please explain what this means. What was expected? What happened instead?

---

<div class="post-metadata">

### Author: ![Sokariz](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Sokariz](https://forums.powershell.org/u/Sokariz)
#### Post date: [August 3, 2023, 7:48am UTC](https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701/7 "2023-08-03T07:48:58Z")

</div>

Good morning,  
Yes sorry, I expected it to take every Ip adresses, add +1 at the end and check if it was in my ip adress list. For exemple it should analyze 10.32.151.20, add + 1 to the last octet then check if 10.32.151.21 is on the list given by the cmdlet “Get-DhcpServerv4Lease”(at the beginning of my code). If the IP address is on the list it does nothing, if not it should display it in the chart.  
That’s why I tried to use every last octet as int and not string.  
Instead I got an error message telling me that it couldn’t convert the every value in “System.Int32”. Error: the input string is incorrect for $lastoctet = [int]$octects[-1].

```auto
"Impossible de convertir la valeur « 99} » en type « System.Int32 ». Erreur : « Le format de la chaîne d'entrée est incorrect. »
Au caractère C:\Users\myname\Documents\script\testchiffrred.ps1:6 : 5
+ $lastOctet = [int]$octets[-1]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument : (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

```

I apologize as it is writen in french and I thank you for your kind help.

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [August 3, 2023, 10:12am UTC](https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701/8 "2023-08-03T10:12:56Z")

</div>

> [@Sokariz](#):
>
> For security reasons they have practically removed the main purpose of a dhcp server.

What a bullshit. 🙄

> [@Sokariz](#):
>
> I expected it to take every Ip adresses, add +1 at the end and check if it was in my ip adress list. For exemple it should analyze 10.32.151.20, add + 1 to the last octet then check if 10.32.151.21 is on the list given by the cmdlet “Get-DhcpServerv4Lease”(at the beginning of my code). If the IP address is on the list it does nothing, if not it should display it in the chart.

I think you are overcomplicating this. 😏

I assume you have the information about the IP ranges you use. And I understood that you can get the IP leases. That’s all you need I’d think.

Let’s say you want to check the IP range from `192.168.0.1` to `192.168.0.32`. Then you simply create a reference like this:

```auto
$IPRange = 1..32 |
    ForEach-Object {
        '192.168.0.{0}' -f $_
    }

```

Let’s say you have got the IP leases in a variable `$IPLeases`:

```auto
$IPLeases = 
'192.168.0.9',
'192.168.0.8',
'192.168.0.17',
'192.168.0.16',
'192.168.0.25',
'192.168.0.24',
'192.168.0.31'

```

Now you simply compare theses two variables with `Compare-Object`

```auto
Compare-Object -ReferenceObject $IPRange -DifferenceObject $IPLeases -IncludeEqual

```

I included the parameter `-IncludeEqual` just to show the general function. Of course you could omit it since you’re interested in the free IP addresses.  
The output would be this:

```auto
InputObject SideIndicator
----------- -------------
192.168.0.8 ==
192.168.0.9 ==
192.168.0.16 ==
192.168.0.17 ==
192.168.0.24 ==
192.168.0.25 ==
192.168.0.31 ==
192.168.0.1 <=
192.168.0.2 <=
192.168.0.3 <=
192.168.0.4 <=
192.168.0.5 <=
192.168.0.6 <=
192.168.0.7 <=
192.168.0.10 <=
192.168.0.11 <=
192.168.0.12 <=
192.168.0.13 <=
192.168.0.14 <=
192.168.0.15 <=
192.168.0.18 <=
192.168.0.19 <=
192.168.0.20 <=
192.168.0.21 <=
192.168.0.22 <=
192.168.0.23 <=
192.168.0.26 <=
192.168.0.27 <=
192.168.0.28 <=
192.168.0.29 <=
192.168.0.30 <=
192.168.0.32 <=

```

… and is easily processable with PowerShell. 😉

---

<div class="post-metadata">

### Author: ![Sokariz](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Sokariz](https://forums.powershell.org/u/Sokariz)
#### Post date: [August 3, 2023, 1:37pm UTC](https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701/9 "2023-08-03T13:37:13Z")

</div>

Woaw… THANK YOU. It’s almost what I am looking for, it does help a lot. It works for your code but for mine as I get my ip address list from the Get-DhcpServerv4Lease it appears as such :

```auto
@{IPAddress=172.16.251.208} =>
@{IPAddress=172.16.251.209} =>
@{IPAddress=172.16.251.210} =>
@{IPAddress=172.16.251.243} =>
@{IPAddress=172.16.251.249} =>
172.16.251.208 <=
172.16.251.209 <=
172.16.251.210 <=
172.16.251.211 <=
172.16.251.212 <=
172.16.251.213 <=
172.16.251.214 <=
172.16.251.215 <=
172.16.251.216 <=
172.16.251.217 <=
         

```

Some of the IP addresses listed here are used and listed in the Lease.  
In my code I have used :

```auto
$ipa = Get-DhcpServerv4Lease -ComputerName "servername" -ScopeId 172.16.251.0 | Select-Object IPAddress

```

The displayed result is as such :

```auto
172.16.251.208
172.16.251.209
172.16.251.210
172.16.251.243
172.16.251.249

```

There isn’t a table format… So I don’t really understand why the data return differently  
I cannot thank you enough though.

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [August 3, 2023, 1:40pm UTC](https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701/10 "2023-08-03T13:40:24Z")

</div>

> [@Sokariz](#):
>
> In my code I have used :
> 
> ```auto
> $ipa = Get-DhcpServerv4Lease -ComputerName "servername" -ScopeId 172.16.251.0 | Select-
> 
> ```

Change it to:

```auto
$ipa = 
    Get-DhcpServerv4Lease -ComputerName "servername" -ScopeId 172.16.251.0 | 
        Select-Object -ExpandProperty IPAddress

```

---

<div class="post-metadata">

### Author: ![Sokariz](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Sokariz](https://forums.powershell.org/u/Sokariz)
#### Post date: [August 3, 2023, 1:50pm UTC](https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701/11 "2023-08-03T13:50:58Z")

</div>

Thank you very much ! It works ! I have another question as a student : How can I get efficiently better at scripting in powershell, as it is hard to find a place where informations about it are very complete. I find your knowledge truly astonishing !

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [August 3, 2023, 2:35pm UTC](https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701/12 "2023-08-03T14:35:04Z")

</div>

> [@Sokariz](#):
>
> How can I get efficiently better at scripting in powershell

Practice, practice, practice. 🤷🏼‍♂️

Find a problem you think you mey solve with a script and start googling if there’s already some code available for. If you find something - learn from it. And maybe improve it or adapt it to your particular needs. There is no shortcut where you get all needed information at one place. Sorry.

Just for your reference … I do PowerShell for about 16 years now. 😉

---

<div class="post-metadata">

### Author: ![Sokariz](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Sokariz](https://forums.powershell.org/u/Sokariz)
#### Post date: [August 3, 2023, 2:48pm UTC](https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701/13 "2023-08-03T14:48:08Z")

</div>

> [@Olaf](#):
>
> I do PowerShell for about 16 years now. 😉

Wow ok I understand why now ! Thank you for your advices I’ll do my best to improve myself ! I don’t really like asking for help and bother people haha

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/get-free-ip-address-in-a-dhcp-server-in-reservation-mode/22701/14 "2024-05-16T20:14:45Z")

</div>


