|
| 1 | +# Export user count in OU |
| 2 | + |
| 3 | +``` |
| 4 | +Import-Module activeDirectory |
| 5 | +
|
| 6 | +#Populate variables |
| 7 | +$output = Read-Host "'Y' for output to file or any key for output in GUI table view" -foreground Cyan |
| 8 | +$fqdn = Read-Host "Enter FQDN domain" |
| 9 | +$cred = Get-Credential |
| 10 | +
|
| 11 | +#Contact the DC |
| 12 | +Write-Host "Contacting $fqdn domain..." -ForegroundColor Yellow |
| 13 | +
|
| 14 | +#Populate the $domain variable |
| 15 | +$domain = (get-addomain $fqdn -Credential $cred | select distinguishedName,pdcEmulator,DNSroot,DomainControllersContainer) |
| 16 | +
|
| 17 | +#Inform user that we're calculating the OUs |
| 18 | +Write-Host "Completed. Enumerating OUs.." -ForegroundColor Yellow |
| 19 | +
|
| 20 | +#Populate the $OUlist variable with array data |
| 21 | +$OUlist = @(Get-ADOrganizationalUnit -filter * -Credential $cred -SearchBase $domain.distinguishedName -SearchScope Subtree -Server $domain.DNSroot) |
| 22 | +
|
| 23 | +#Inform user that we're now counting the users |
| 24 | +Write-Host "Completed. Counting users..." -ForegroundColor Yellow |
| 25 | +
|
| 26 | +#Create a progress bar counting the OUs |
| 27 | +for($i = 1; $i -le $oulist.Count; $i++) |
| 28 | + {write-progress -Activity "Collecting OUs" -Status "Finding OUs $i" -PercentComplete ($i/$OUlist.count*100)} |
| 29 | +$newlist = @{} |
| 30 | + |
| 31 | + |
| 32 | +#Count the number of users in each OU list created from the $OUlist variable |
| 33 | +foreach ($_objectitem in $OUlist) |
| 34 | + { |
| 35 | + $getUser = Get-ADuser -Filter * -Credential $cred -SearchBase $_objectItem.DistinguishedName -SearchScope OneLevel -Server $domain.pdcEmulator | measure | select Count |
| 36 | + for($i = 1; $i -le $getUser.Count; $i++) |
| 37 | + {write-progress -Activity "Counting users" -Status "Finding users $i in $_objectitem" -PercentComplete ($i/$getUser.count*100)} |
| 38 | + |
| 39 | + $newlist.add($_objectItem.DistinguishedName, $getUser.Count) |
| 40 | + } |
| 41 | + |
| 42 | +#Either write to file or write to console depending on the $output variable populated at the start |
| 43 | + if ($output -eq "Y") |
| 44 | + { |
| 45 | + $newlist | ft -AutoSize | Out-File .\OUuserCount.txt |
| 46 | + Write-Host "All done!" -ForegroundColor yellow |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + $newList | Out-GridView |
| 51 | + } |
| 52 | +``` |
| 53 | + |
0 commit comments