Find IP addresses available in a list

Welcome Forums General PowerShell Q&A Find IP addresses available in a list

This topic contains 7 replies, has 5 voices, and was last updated by

3 years, 7 months ago.

  • Author
  • #29068

    Points: 63
    Rank: Member

    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:

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

  • #29069

    Points: 6
    Rank: Member

    Is the input list available addresses or used addresses?

  • #29070

    Points: 150
    Team Member
    Rank: Participant

    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 1

    To this:

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

    Points: 63
    Rank: Member

    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:

  • #29072

    Points: 639
    Helping Hand
    Rank: Major Contributor

    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
  • #29073

    Points: 6
    Rank: Member

    $UsedIPs = @'

    $UsedOctets = $UsedIPs.Split( "`n" ) | ForEach { [int]$_.Split( '.' )[-1] }

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


  • #29110

    Points: 0
    Rank: Member

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

    $usedIps = @'
    '@ -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 $_"
            "Next IP is $net$($ptr + 1)"
  • #29149

    Points: 63
    Rank: Member

    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!

The topic ‘Find IP addresses available in a list’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort