1

I've written two scripts that give me the independent output that I need, but am not sure how to put them together to give me the combined output.

Returns the count of all of the active users in a particular OU

(Get-ADUser -searchbase "OU=OU, DC=domain, DC=com" -filter * |Where {$_.enabled -eq "True"}).count

Returns the OU's underneath the same above OU that have "string" in the description property.

Get-ADOrganizationalUnit -searchbase "OU=OU, DC=domain, DC=com" -filter * -Properties description | where {$_.description -eq "string"}

What I'm trying to accomplish is for the script to give me a count of all of the active users underneath the OU's that have "string" in the description property.

2 Answers 2

1

I think this is the easiest way to achieve your goal

$OUs = Get-ADOrganizationalUnit -searchbase "OU=OU, DC=domain, DC=com" -filter * -Properties description | where {$_.description -eq "string"}
ForEach ($OU in $OUs) {
    $count = (Get-ADUser -searchbase $OU -filter * | Where {$_.enabled -eq "True"}).count
    Write-Host "OU $OU has $count users"
}

the result will be

OU OU=foo,DC=domain,DC=com has 6 users
OU OU=Computers,OU=foo,DC=domain,DC=com has 0 users
OU OU=Users,OU=foo,DC=domain,DC=com has 6 users
OU OU=Groups,OU=foo,DC=domain,DC=com has 0 users
Sign up to request clarification or add additional context in comments.

Comments

1

Sergio Tanaka's helpful answer works well; let me complement it with a performance improvement:

You can greatly speed up your command by filtering at the source, by passing the filter criterion as a -Filter argument instead of retrieving all objects first and then filtering them with a separate Where-Object call:

Get-ADOrganizationalUnit -SearchBase 'OU=OU, DC=domain, DC=com' `
                        -Filter  'Description -eq "string"' -Properties description |   #`
  ForEach-Object {
    $count = (Get-ADUser -SearchBase $_ -Filter 'Enabled -eq $true').Count
  }

Note that the -Filter-string syntax of the AD cmdlets resembles PowerShell code, but it differs in many important ways - see Get-Help about_ActiveDirectory_Filter


The general advantages of using -Filter:

On a general note, the same performance improvement can be had with cmdlets for other PowerShell data providers, such as the one for the filesystem (e.g., Get-ChildItem), if they support a -Filter parameter:

  • A -Filter string is applied at the data source, which means that PowerShell only receives the result of the filtering.

    • Since providers are are implemented in compiled code (and they have access to lower-level internals), this generally makes for much better performance; additionally, in remoting scenarios performance improves by simply having to transfer less data over the network.
  • Note that -Filter parameters are always strings with provider-specific syntax, so you must consult the relevant provider/cmdlet documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.