How to display a range of count values

I am having a serious brain fart right now and I know this is quite simple, but I just cannot for the life of me conceptualize this.

How would I show only values that fall within the range of say something like 10 through 50 in the following example:

$GetADComputersList = Get-ADObject -Filter 'ObjectCategory -eq "Computer"' -SearchBase "OU=My,DC=test,DC=domain"

$GetADComputersList.Count

I was thinking about using the range operator, but this did not work as I had intended.

Thanks for any help I can get

Please try if below works for you.

$Result = ($GetADComputersList.Count -in 10..50)
$Result

thanks for the reply. No that did not work either. It shows False as the output

What is the exact output you’ve expect from this?

if ($GetADComputersList.Count -in 10..50)
{
  $GetADComputersList.Count
}

And why are you not using Get-ADComputer?

$GetADComputersList = Get-ADComputer -SearchBase "OU=My,DC=test,DC=domain"
if ($GetADComputersList.Count -in 10..50)
{
  $GetADComputersList.Count
}

The output I would like to see is a specified range (number) of computers that exist within OUs. I have most of my code written and it is working great, but I want to be able to choose what values I want to display. For instance, I don’t want to see 0 values. Perhaps I only want to see the OUs that have between 10 to 20 computers in them.

The reason I am not using Get-ADComputer is because I am not using this script to find JUST computer objects. I am using it to also find Group, Organizational Unit, and User objects as well.

For instance, this code here works perfect, but I simply want to be able to choose a range, or a starting value so I can determine what values I want displayed:

$Object = "Computer"

$BaseOU = "OU=My,DC=test,DC=domain"

$OUProps = Get-ADObject -Filter 'ObjectCategory -eq "OrganizationalUnit"' -SearchBase $BaseOU -Properties CanonicalName, DistinguishedName, Name 


$GetCount = Foreach ($OUDN in $OUProps.DistinguishedName)
{
    (Get-ADObject -Filter 'ObjectCategory -eq $Object' -SearchBase $OUDN).Count 
}
 

 $GetCount

Thanks for the better explanation. I hope the following code works for you and produces the expected output.

$ObjectClass = "Computer"
$BaseOU = "OU=My,DC=test,DC=domain"
$OUs = Get-ADObject -Filter 'ObjectCategory -eq "OrganizationalUnit"' -SearchBase $BaseOU -Properties CanonicalName, DistinguishedName, Name

$Results = Foreach ($OU in $OUs)
{
    $Count = @(Get-ADObject -Filter 'ObjectCategory -eq $ObjectClass' -SearchBase $OU.DistinguishedName).Count

    $Result = Select-Object -InputObject $OU -Property CanonicalName, DistinguishedName, Name
    Add-Member -InputObject $Result -MemberType NoteProperty -Name ChildObjectCount -Value $Count
    Add-Member -InputObject $Result -MemberType NoteProperty -Name ChildObjectClass -Value $ObjectClass

    Write-Output -InputObject $Result
}

# Output objects with the ChildObjectCount within the range 10 to 50
$Results | Where-Object { $_.ChildObjectCount -in 10..50 }

# Output objects with the ChildObjectCount greater than or equal to 10
$Results | Where-Object { $_.ChildObjectCount -ge 10 }

Yep, that worked GREAT!

Thanks

Dan,

How do I get this to show ONLY “0” values as well?

I tried to use 0…0, but I am guessing the reason it did not show any values is due to the fact that there is no range between 0 and 0.

Thanks

Disregard, I got it

$GetADComputersList = Get-ADObject -Filter ‘ObjectCategory -eq “Computer”’ -SearchBase “OU=My,DC=test,DC=domain”
$GetADComputersList | Select -First 50

Guess this is what you are looking for?