How can I export the users of an OU and its child OUs?

Answer-SeekerPS 0 Reputation points
2023-03-02T08:13:12.7466667+00:00

I have an OU with many children and I don't want to mnaully export a hundred objects and compile the users in a list. I have the below script that runs without error, but exports an empty file. Can someone help me to know what's wrong with it?

# Define the path and file name for the CSV export 
$ExportPath = "C:\temp\OU-Export.csv"

# Create an array to store the OU data
$OUs = @()

# Set the FGH OU distinguished name manually
$OU = "OU=FGH,DC=FGH,DC=local"

# Get all of the child OUs of the FGH OU
$Searcher = New-Object System.DirectoryServices.DirectorySearcher
$Searcher.Filter = "(&(objectClass=organizationalUnit)(parentDN=$OU))"
$Searcher.SearchScope = "OneLevel"
$Results = $Searcher.FindAll()

# Loop through the child OUs and get the users within
foreach ($Result in $Results) {
    $ChildOU = $Result.Properties.distinguishedname
    $Searcher.Filter = "(&(objectClass=user)(objectCategory=person)(memberof:1.2.840.113556.1.4.1941:=$ChildOU))"
    $Users = $Searcher.FindAll()

    # Add the child OU and user data to the array
    foreach ($User in $Users) {
        $UserProperties = @{
            "OU" = $ChildOU
            "Name" = $User.Properties.samaccountname
            "Description" = $User.Properties.description
            "Email" = $User.Properties.mail
        }
        $OUs += New-Object PSObject -Property $UserProperties
    }
}

# Export the array to a CSV file
$OUs | Export-Csv -Path $ExportPath -NoTypeInformation

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,245 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,329 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Andrés López 1 Reputation point
    2023-03-02T09:27:49.1266667+00:00

    Hi,

    Have you tried running the query with the Get-Aduser cmdlet? You can specify a base OU and it returns child objects,

    If the answer is helpful, please click "Accept Answer"

    # Define the path and file name for the CSV export 
    $ExportPath = "C:\OU-Export.csv"
    
    # Create an array to store the OU data
    $OUs = @()
    
    # Set the FGH OU distinguished name manually
    $OU = "OU=FGH,DC=FGH,DC=local"
    
    # Get all of the child OUs of the FGH OU
    $Searcher = Get-ADUser -Filter * -SearchBase $OU -Properties * | select *
        
    foreach ($User in $Searcher) {
            $UserProperties = @{
                "OU" = $User.DistinguishedName
                "Name" = $User.Name
                "Description" = $User.description
                "Email" = $User.mail
            }
            $OUs += New-Object PSObject -Property $UserProperties
    
    }
    
    # Export the array to a CSV file
    $OUs | Export-Csv -Path $ExportPath -NoTypeInformation
    
    0 comments No comments

  2. Andrés López 1 Reputation point
    2023-03-02T09:34:58.3466667+00:00

    Have you tried getting the results with the Get-Aduser cmdlet and Searchscope? You can specify a base OU and it returns the child objects:

    # Define the path and file name for the CSV export 
    $ExportPath = "C:\OU-Export.csv"
    
    # Create an array to store the OU data
    $OUs = @()
    
    # Set the FGH OU distinguished name manually
    $OU = "OU=FGH,DC=FGH,DC=local"
    
    # Get all of the child OUs of the FGH OU
    $Searcher = Get-ADUser -Filter * -SearchBase $OU -Properties * | select *
    
    # Loop through the child OUs and get the users within
        # Add the child OU and user data to the array
    foreach ($User in $Searcher) {
            $UserProperties = @{
                "OU" = $User.DistinguishedName
                "Name" = $User.Name
                "Description" = $User.description
                "Email" = $User.mail
            }
            $OUs += New-Object PSObject -Property $UserProperties
    
    }
    
    # Export the array to a CSV file
    $OUs | Export-Csv -Path $ExportPath -NoTypeInformation
    

    If the answer is helpful, please click "Accept Answer"

    0 comments No comments

  3. Rich Matheisen 45,906 Reputation points
    2023-03-02T19:08:46.43+00:00

    This will get the users in the $OU organizational unit and all of the children of the $OU organizational unit. The setting of the "-SearchScope" parameter of the Get-ADUser cmdlet takes care of that.

    The Organizational Unit is separated from the users' distinguishedName by a regular expression that also correctly handles any embedded commas in the CN portion of the distinguishedName.

    It doesn't need any intermediate storage in arrays, but relies only on the use of PowerShells' pipeline.

    # Define the path and file name for the CSV export 
    $ExportPath = "C:\temp\OU-Export.csv"
    
    # Set the FGH OU distinguished name manually
    $OU = "OU=FGH,DC=FGH,DC=local"
    
    Get-ADUser -Filter * -Properties Description,Email -SearchBase = $OU -SearchScope Subtree |
        ForEach-Object{
            [PSCustomObject]@{
                "OU" = $_.distinguishedName -replace '^CN=.+?(?<!\\),(.+)$','$1'  # get all EXCEPT CN value
                "Name" = $_.samaccountname
                "Description" = $_.description
                "Email" = $_.mail
            }
        } | Export-Csv -Path $ExportPath -NoTypeInformation
    
    
    0 comments No comments