Author: Ron Ratzlaff (aka “The_Ratzenator”)

Date: 10\17\2014

Count AD ObjectsIn OU PowerShell Tool Help File

The help information provided below can also be viewed by using the following syntax:

Get-HelpCount-ADObjectsInOU–Full

Help Info

NAME

Count-ADObjectsInOU

SYNOPSIS

Retrieves a count of specified Active Directory objects

SYNTAX

Count-ADObjectsInOU [-Object] <String> [-BaseOU <String>]

[-StartingCountValue <Object>] [-EndingCountValue <Object>]

[-ShowOnlyCountValue <Object>] [<CommonParameters>]

DESCRIPTION

The "Count Active Directory Objects in an OU PowerShell Tool” utilizes the

"Get-ADObject" cmdlet and uses its filter parameter to target the

"ObjectCategory attribute. The objects that can be searched for are:

Computer, Group, Organizational Unit, and User. This tool retrieves the

count values of specified AD objects by using the mandatory “Object"

parameter. If you want to specify a certain OU that you would like to

count AD objects within, then you can use the “BaseOU" parameter. The

“BaseOU" parameter is used by the "Get-ADOjbect” cmdlet's “SearchBase"

parameter, so if there are multiple OUs as child objects within the

specified OU, a recursive search is done and count values will be

displayed for the specified AD object within those OUs as well. The

“Object" parameter is the only mandatory parameter and can be used by

itself to get all count values of specified AD ojbects within your domain

hierarchy. If you prefer to narrow the count values and just find AD

objects within a certain range of values, then you can use the

“StartingCountValue" and “EndingCountValue” range parameters. The

“StartingCountValue" and the “EndingCountValue" parameters cannot have the

same value, or an error will be displayed since, naturally, ranges do not

exist between the same number. The “StartingCountValue” and the

“EndingCountValue” also cannot have a value of “0”, or an error will be

displayed. The “EndingCountValue” parameter cannot be used without the

“StartingCountValue” parameter. However, the "StartignCountValue"

parameter can be used without the "EndingCountValue" to create a starting

point value to find the object count that is greater than or equal to the

"StartignCountValue" value. If the “BaseOU" parameter is used to define

which OU to search in, the script will first validate whether the

DistinguishedName value that is entered for the “BaseOU" parameter

actually exists before moving further in the script process. If a typo, or

non-existent OU is entered as the DistinguishedName for the “BaseOU"

parameter, an error will display. If you prefer to narrow the search down

even more and would like to search for one specific count value, then you

can use the “ShowOnlyCountValue” parameter. You may also use the

“ShowOnlyCountValue” parameter along with the two range parameters

(“StartingCountValue" and “EndingCountValue”). The “ShowOnlyCountValue”

parameter, unlike the two range parameters, can have a value of “0”.

PARAMETERS

-Object <String>

The only mandatory parameter that is used to specifiy the Active

Directory Object Category object.

Required? true

Position? 1

Default value

Accept pipeline input? false

Accept wildcard characters? false

-BaseOU <String>

An optional parameter used to specify the starting OU to search within

for specified objects. The "BaseOU" parameter can be used with all

other parameters.

Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false

-StartingCountValue <Object>

An optional parameter used to find specified objects with a specfied

starting count value. The "StartingCountValue" parameter can be used

with all other parameters. The "StartingCountValue" cannot have a

value of "0" or be equal to the value of the "EndingCountValue"

parameter or an error will result.

Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false

-EndingCountValue <Object>

An optional parameter used to find specified objects within a specifed

range of values. The "EndingCountValue" parameter cannot be used by

itself and must be used with the "StartingCountValue" parameter, or an

error will result. Also, The "EndingCountValue" cannot have a value of

"0" or be equal to the value of the "StartingCountValue" parameter or

an error will result.

Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false

-ShowOnlyCountValue <Object>

An optional parameter used to find specified objects with an exact

count value. The "ShowOnlyCountValue" parameter can be used with all

other parameters.

Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false

<CommonParameters

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer and OutVariable. For more information, see

about_CommonParameters

(

INPUTS

OUTPUTS

------EXAMPLE 1 ------

C:\PS>Retrieve a count for specified objects in every OU in the domain

hierarchy:

Computer Objects

------

Count-ADObjectsInOU -Object "computer"

Group Objects

------

Count-ADObjectsInOU -Object "group"

Organizational Unit Objects

------

Count-ADObjectsInOU -Object "organizationalunit"

User Objects

------

Count-ADObjectsInOU -Object "user"

------EXAMPLE 2 ------

C:\PS>Retrieve a count for specified objects in every OU in the domain

hierarchy between a range of 10 and 200:

Computer Objects

------

Count-ADObjectsInOU -Object "computer" -StartingCountValue "10"

-EndingCountValue "200"

Group Objects

------

Count-ADObjectsInOU -Object "group" -StartingCountValue "10"

-EndingCountValue "200"

Organizational Unit Objects

------

Count-ADObjectsInOU -Object "organizationalunit" -StartingCountValue

"10" -EndingCountValue "200"

User Objects

------

Count-ADObjectsInOU -Object "user" -StartingCountValue "10"

-EndingCountValue "200"

Using Parameter Aliases

------

Count-ADObjectsInOU -Object "computer" -SCV "10" -ECV "200"

------EXAMPLE 3 ------

C:\PS>Retrieve a count for a specified object in every OU in the domain

hierarchy between a range of 10 and 200 and show all OUs where a computer

object count value of "0" exist:

Count-ADObjectsInOU -Object "computer" -StartingCountValue "10"

-EndingCountValue "200" -ShowOnlyCountValue "0"

Using Parameter Aliases

------

Count-ADObjectsInOU -Object "computer" -SCV "10" -ECV "200" -SOCV "0"

------EXAMPLE 4 ------

C:\PS>Retrieve specified objects where the count values are greater than

or equal to the starting count value of 5:

Count-ADObjectsInOU -Object "computer" -StartingCountValue "5"

Using Parameter Aliases

------

Count-ADObjectsInOU -Object "computer" -SCV "5"

------EXAMPLE 5 ------

C:\PS>Retrieve specified objects where the count values are greater than

or equal to the starting count value of 100 and also show only counts

where the value is 20:

Count-ADObjectsInOU -Object "computer" -StartingCountValue "100"

-ShowOnlyCountValue "20"

Using Parameter Aliases

------

Count-ADObjectsInOU -Object "computer" -SCV "100" -SOCV "20"

------EXAMPLE 6 ------

C:\PS>Use the "BaseOU" parameter to narrow the output to specified OUs

using their Distinguished Name for all previous examples shown above. For

example, to find computer objects within the "My" OU and its child OUs in

the "test.com" domain with a range of values between 1 and 250 and a show

only count value of 50:

Count-ADObjectsInOU -Object "computer" -BaseOU "OU=My,DC=test,DC=com"

-StartingCountValue "1" -EndingCountValue "250" -ShowOnlyCountValue "20"

Using Parameter Aliases

------

Count-ADObjectsInOU -Object "computer" -BOU "OU=My,DC=test,DC=com" -SCV

"100" -ECV "250" -SOCV "20"

------EXAMPLE 7 ------

C:\PS>To output the results to a file as well as to the screen:

Count-ADObjectsInOU -Object "Computer" | Tee-Object -FilePath

"$env:userprofile\desktop\Count-ADObjectsInOU.log"

------EXAMPLE 8 ------

C:\PS>To output the results to a file only and not output to the screen:

Count-ADObjectsInOU -Object "Computer" | Out-File -FilePath

"$env:userprofile\desktop\Count-ADObjectsInOU.log"

RELATED LINKS