How to get list of Empty OU

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 8 months ago.

  • Author
    Posts
  • #6237

    by Nidhin CK at 2012-12-19 14:10:34

    Could someone help me to modify the below script which i wrote for finding the empty OU's in AD. Im new to this powershell .. below code gives results but this will not work in all condition, for eg if an OU has more than one sub OU

    cls
    Import-Module activedirectory
    $ou = Get-ADOrganizationalUnit -Filter {name -eq "che"} -Properties distinguishedname
    $allou = Get-ADObject -Filter {objectclass -eq "organizationalUnit"} -SearchBase $ou
    foreach ($singleou in $allou){
    $objects = Get-ADObject -Filter * -SearchBase $singleou
    $a = @($objects).length
    if ($a -gt 2){
    Write-Output "$objects is is not Empty" | Out-File c:\output.txt -Append
    }
    else{
    Write-Output "$objects empty" | Out-File c:\output.txt -Append
    }
    }

    by Infradeploy at 2012-12-20 03:44:35

    $ou is an array.

    So it's:

    $ous = Get-ADOrganizationalUnit -Filter {name -eq "che"} -Properties distinguishedname
    foreach$ou in $ous){
    $allou = Get-ADObject -Filter {objectclass -eq "organizationalUnit"} -SearchBase $ou
    foreach ($singleou in $allou){
    $objects = Get-ADObject -Filter * -SearchBase $singleou
    $a = @($objects).length
    if ($a -gt 2){
    Write-Output "$objects is is not Empty" | Out-File c:\output.txt -Append
    }
    else{
    Write-Output "$objects empty" | Out-File c:\output.txt -Append
    }
    }
    }

You must be logged in to reply to this topic.