Source Code and Samples

Thursday, 8 February 2007

List Users from a Organizational Unit (OU)

Description

List Users from a Organizational Unit (OU).

Source Code

$Dom = "LDAP://OU=OfficeUsers,DC=powershellscripts,DC=blogspot,DC=com"
$Root = New-Object DirectoryServices.DirectoryEntry $Dom

# Create a selector and start searching from the Root of AD
$selector = New-Object DirectoryServices.DirectorySearcher
$selector.SearchRoot = $root
# Filter the users with -like "CN=Person*". Note the ForEach loop
$adobj= $selector.findall() where {
$_.properties.objectcategory -like "CN=Person*"
}
foreach ($person in $adobj)
{
$prop=$person.properties
Write-host "First name: $($prop.givenname) Surname: $($prop.sn) User: $($prop.cn)"
}
Write-host "There are $($adobj.count) users in the $($root.name) domain"

3 comments:

Unknown said...

TYPO - should be:
| where
not just
where

Vinch.be said...

Thanks for the script

Boikanyo said...

Thanks for the script - but it only counts the number of "Person" objects in that specific OU.