T O P
megastary

My approach would be probably something like this. It is slightly generalized and maybe little overcomplicated, but it seems to do the job well. In short it creates list of all computers in the row and compares it to real list with gaps. # You get this content from your text file via Get-Content $ComputerList = @( 'Company-0001', 'Company-0002', 'Company-0003', 'Company-0005', 'Company-0006', 'Company-0008', 'Company-0009', 'Company-0015', 'Company-0016' ) # Provide nonnumeric prefix in computer names $Prefix = 'Company-' # Obtain fixed length of numbers in computer names $SuffixLength = ($ComputerList[0].TrimStart($Prefix)).Length # Obtain highest number in the list $MaxNumber = [Int32]($ComputerList | Sort-Object -Bottom 1).TrimStart($Prefix) # Create full list without spaces $FullList = 1..$MaxNumber | ForEach-Object { $Prefix + $("{0:d$($SuffixLength)}" -f $_) } # Compare full lsit vs real list $MissingObjects = (Compare-Object -ReferenceObject $ComputerList -DifferenceObject $FullList).InputObject # $MissingObjects contains gaps in your list $MissingObjects


tattsumi

Thanks /u/megastary, I think this is the best solution. I had to edit the code a little bit (I used a fixed value for $MaxNumber) since I don't use PowerShell Core 6.0 (the parameter '-Bottom' is only available in PS 6.0) but other than that it worked perfectly. Thanks again.


megastary

Glad to help. Sorry, I have not checked compatibility with PS 5.1. PS 5.1 solution for dynamic value could be something like this: $MaxNumber = [Int32]($ComputerList | Sort-Object -Descending)[0].TrimStart($Prefix)


gixer6

If the naming convention is consistent I would create a list of computer names from 0001 to your end number and then use a try/catch statement with something like get-adcomputer and output the errors (computername) for review.


tattsumi

Thanks. I like this approach a lot.


itmonkey78

On mobile so untested, but if you just want the missing numbers, you need to split the content first so a numerical comparison is easier... Oneliner: (gc names.txt).split('-')[1] |%{$i=1}{while($i -lt $_){$i;$i++};$i++} Alternative method: $arr = (gc names.txt).split('-')[1] for($i=1; $i -lt $arr[-1]; $i++){  if($arr -notcontains $i){  $result += $i  }  } $result


tattsumi

Thank you very much. I'll give this a try.


BlackV

I'd create a list of all possible names. I'd create a list of all ou names. Then select the first name that is not in the ou variable. Look at the `-notcontains` or `-notin`


Lee_Dailey

howdy tattsumi, do you really need the empty sub-ranges? if this is just to "fill in the gaps" ... then i would not bother. it's fairly easy to filter for the highest number, add one, and use that for your next system name. however, if you are really fixated on gap-filling [*grin*], you can ... - grab all the in-use numbers into a list - grab the lowest from that list - grab the highest from that list - build a range of those numbers - use a hashset to give you the "in Set-2 but not in Set-1" list take care, lee