function Get - NextComputerName {

#Workstation Name Scheme IE: NFDW0001

$sitecode = "NFDW"

$searcher = new-object System.DirectoryServices.DirectorySearcher ( "(&(objectClass=computer)(name=$sitecode*))" )

$comps = $searcher .findall ( )

$names = $comps | % { $_ .Properties.name }

$numbers = @ ( $names | % { $_ -replace "$sitecode" , "" } | % { [ int ] $_ } | sort )

#make sure single items still get treated as a list

$i = 0

$prev = 0 #Assume you start at "0001"... $prev should be one less than your first expected number

$next = $numbers [ 0 ]

while ( ( $next - $prev ) -le 1 -and $i -le ( $numbers .count - 1 ) ) {

$i ++

$prev = $next

$next = $numbers [ $i ]

}

if ( $i -le $numbers .count ) {

$nextopen = "$sitecode{0:0###}" -f ( $prev + 1 )

} else {

$nextopen = "$sitecode{0:0###}" -f ( $next + 1 )

}

write-host "Available Workstation: $nextopen"

#Mobile Name Scheme IE: NFDM0001

$sitecode = "NFDM"

$searcher = new-object System.DirectoryServices.DirectorySearcher ( "(&(objectClass=computer)(name=$sitecode*))" )

$comps = $searcher .findall ( )

$names = $comps | % { $_ .Properties.name }

$numbers = @ ( $names | % { $_ -replace "$sitecode" , "" } | % { [ int ] $_ } | sort )

#make sure single items still get treated as a list

$i = 0

$prev = 0 #Assume you start at "0001"... $prev should be one less than your first expected number

$next = $numbers [ 0 ]

while ( ( $next - $prev ) -le 1 -and $i -le ( $numbers .count - 1 ) ) {

$i ++

$prev = $next

$next = $numbers [ $i ]

}

if ( $i -le $numbers .count ) {

$nextopen = "$sitecode{0:0###}" -f ( $prev + 1 )

} else {

$nextopen = "$sitecode{0:0###}" -f ( $next + 1 )

}

write-host "Available Mobile: $nextopen"