These scripts give examples on how to use the Active Directory module for Windows PowerShell, or the snap-in Quest Active Roles Management, to check if a user object in AD DS has child objects. If so, the script deletes the child objects.

A procedure to delete child objects is useful in context of user migration with Active Directory Migration Tool (ADMT), who can’t migrate user objects with child objects.

PowerShell
Edit|Remove
# Microsoft Active Directory Module Example: 
function Del-LeafObject  
{ 
    <# 
    .SYNOPSIS 
    Del-LeafObject 
    .DESCRIPTION 
    Removes Child object from userobjects 
    .NOTES 
    AUTHOR:    Lars Jostein Silihagen, lars.jostein@silihagen.net 
    #> 
    param  
    ( 
        [string]$ADUser 
    ) 
    $LeafObject = Get-ADObject -Filter * -SearchScope oneLevel -SearchBase $ADUser.DistinguishedName         
    if($LeafObject)     
    {         
        Write-Host "Removes Child Objects for user "  $ADUser -ForegroundColor Yellow 
        $LeafObject | Remove-ADObject -Recursive     
    } 
    else 
    { 
        write-host "Can't find any child objects for user: " $UserName  
    } 
} # end Del-LeafObject 
 
#**************************************************************************** 
 
# Quest Active Roles Management Example: 
function Del-LeafObject 
{ 
    <# 
    .SYNOPSIS 
    Del-LeafObject 
    .DESCRIPTION 
    Removes Child object from userobjects 
    .NOTES 
    AUTHOR:    Lars Jostein Silihagen, lars.jostein@silihagen.net 
    #> 
    param  
    ( 
        [string]$ADUser 
    ) 
    $LeafObject = Get-QADObject -SearchScope onelevel -SearchRoot $ADuser 
    if($LeafObject)     
    {        
        Write-Host "Removes Child Objects for user" $ADUser -ForegroundColor Yellow 
        Remove-QADObject -Identity $LeafObject -deletetree -force 
    } 
    else 
    { 
        write-host "Can't find any child objects for user: " $UserName  
    } 
} # end Del-LeafObject