# Find IP addresses available in a list

**URL:** https://forums.powershell.org/t/find-ip-addresses-available-in-a-list/4771
**Category:** PowerShell Help
**Created:** [August 27, 2015, 5:04am UTC](https://forums.powershell.org/t/find-ip-addresses-available-in-a-list/4771 "2015-08-27T05:04:56Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![vandrey-trindade](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vandrey-trindade/32/198_2.png) [@vandrey-trindade](https://forums.powershell.org/u/vandrey-trindade)
#### Post date: [August 27, 2015, 5:04am UTC](https://forums.powershell.org/t/find-ip-addresses-available-in-a-list/4771/1 "2015-08-27T05:04:56Z")

</div>

Hi again,

I’m trying to create a code that shows me what IPs are available in a range:

Let’s suppose this string variable content:  
`
192.168.47.1
192.168.47.2
192.168.47.3
192.168.47.4
192.168.47.6
192.168.47.7
192.168.47.8
192.168.47.9
192.168.47.14
192.168.47.47
192.168.47.244
`

Is there an easy way to know the first IP address available?

---

<div class="post-metadata">

### Author: ![tim-curwick](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tim-curwick/32/431_2.png) [@tim-curwick](https://forums.powershell.org/u/tim-curwick)
#### Post date: [August 27, 2015, 5:14am UTC](https://forums.powershell.org/t/find-ip-addresses-available-in-a-list/4771/2 "2015-08-27T05:14:36Z")

</div>

Is the input list available addresses or used addresses?

---

<div class="post-metadata">

### Author: ![will-anderson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/will-anderson/32/234_2.png) [@will-anderson](https://forums.powershell.org/u/will-anderson)
#### Post date: [August 27, 2015, 5:15am UTC](https://forums.powershell.org/t/find-ip-addresses-available-in-a-list/4771/3 "2015-08-27T05:15:46Z")

</div>

If it’s being fed into your script as an array, you can always pipe it to Select-Object -First 1 and it’ll give you the first object in your array. If it’s a continuous string, you’ll have to get more creative. For Example:

```
[string]$IPAddress = Get-Content C:\scripts\ipaddress.txt

$IPAddress.Split(' ') | Select-Object -First 1
```

Will change your output from this:

```
PS C:\WINDOWS\system32> $IPAddress
192.168.47.1 192.168.47.2 192.168.47.3 192.168.47.4 192.168.47.6 192.168.47.7 192.168.47.8 192.168.47.9 192.168.47.14 192.168.47.47 1
92.168.47.244
```

To this:

```
PS C:\WINDOWS\system32> $IPAddress.Split(' ')
192.168.47.1
192.168.47.2
192.168.47.3
192.168.47.4
192.168.47.6
192.168.47.7
192.168.47.8
192.168.47.9
192.168.47.14
192.168.47.47
192.168.47.244

PS C:\WINDOWS\system32> $IPAddress.Split(' ') | Select-Object -First 1
192.168.47.1
```

---

<div class="post-metadata">

### Author: ![vandrey-trindade](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vandrey-trindade/32/198_2.png) [@vandrey-trindade](https://forums.powershell.org/u/vandrey-trindade)
#### Post date: [August 27, 2015, 5:32am UTC](https://forums.powershell.org/t/find-ip-addresses-available-in-a-list/4771/4 "2015-08-27T05:32:54Z")

</div>

Tim Curwick,

Used addresses.

Will Anderson,

It’s an array, but I need the first one available.  
In my example it should be IP address: 192.168.47.5

---

<div class="post-metadata">

### Author: ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)
#### Post date: [August 27, 2015, 5:44am UTC](https://forums.powershell.org/t/find-ip-addresses-available-in-a-list/4771/5 "2015-08-27T05:44:58Z")

</div>

This would compare the entire range against what is used and provide the first IP. The only thing that gets a bit more sticky is if you need to sort because you would have to pad each subnet with zero, but this is the general code you’ll need:

```
#Get used IP's from a file
$ipFile = Get-Content C:\Temp\test.txt

#Convert it to a PSObject 
$usedIPs = $ipFile | foreach{
    New-Object -TypeName PSObject -Property @{IP=$_}
}

#Create a PSObject with all possible IPs in the range
$allIPs = for ($i=1;$i -le 255;$i++){
    New-Object -TypeName PSObject -Property @{IP=("192.168.47.{0}" -f $i)}
}

#Compare the objects and that are in the all IP's and not in the used and select the first one
Compare-Object -ReferenceObject $allIPs -DifferenceObject $usedIPs -Property IP -PassThru | 
Where {$_.SideIndicator -eq "<="} |
Select -ExpandProperty IP -First 1
```

---

<div class="post-metadata">

### Author: ![tim-curwick](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tim-curwick/32/431_2.png) [@tim-curwick](https://forums.powershell.org/u/tim-curwick)
#### Post date: [August 27, 2015, 5:56am UTC](https://forums.powershell.org/t/find-ip-addresses-available-in-a-list/4771/6 "2015-08-27T05:56:26Z")

</div>

$UsedIPs = @’  
192.168.47.1  
192.168.47.2  
192.168.47.3  
192.168.47.4  
192.168.47.6  
192.168.47.7  
192.168.47.8  
192.168.47.9  
192.168.47.14  
192.168.47.47  
192.168.47.244  
'@

$UsedOctets = $UsedIPs.Split( “`n” ) | ForEach { [int]$\_.Split( ‘.’ )[-1] }

$FirstAvailableOctet = 1…255 | Where { $UsedOctets -notcontains $\_ } | Select -First 1

“192.168.47.$FirstAvailableOctet”

---

<div class="post-metadata">

### Author: ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)
#### Post date: [August 28, 2015, 2:27am UTC](https://forums.powershell.org/t/find-ip-addresses-available-in-a-list/4771/7 "2015-08-28T02:27:56Z")

</div>

Slightly different approach using the “[version] magic” to split out the octets.

```
$usedIps = @'
192.168.47.1
192.168.47.2
192.168.47.3
192.168.47.4
192.168.47.6
192.168.47.7
192.168.47.8
192.168.47.9
192.168.47.14
192.168.47.47
192.168.47.244
'@ -split "`r`n"
$pattern = [regex]"((\d{1,3}\.){3})"
$ptr = 0
$usedIps[0] -match $pattern | Out-Null
$net = $Matches[1]
$usedIps | foreach {
    if (([version]$_).Revision -eq ($ptr + 1))
    {
        Write-Verbose "Saw $_"
        $ptr++
    }
    else
    {
        "Next IP is $net$($ptr + 1)"
        break
    }
}
```

---

<div class="post-metadata">

### Author: ![vandrey-trindade](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vandrey-trindade/32/198_2.png) [@vandrey-trindade](https://forums.powershell.org/u/vandrey-trindade)
#### Post date: [August 30, 2015, 9:42pm UTC](https://forums.powershell.org/t/find-ip-addresses-available-in-a-list/4771/8 "2015-08-30T21:42:12Z")

</div>

Will Anderson, Tim Curwick, Rob Simmers and Bob McCoy,

Thank you all for the help.  
I’m having some personal problems at the moment and that’s why I took so long to answer.

Thank you once again!

---

<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:44pm UTC](https://forums.powershell.org/t/find-ip-addresses-available-in-a-list/4771/9 "2024-05-16T20:44:50Z")

</div>


