For better organization and performance, you can target specific OUs instead of the entire domain. First, identify your target OU's distinguished name:
Get-ADOrganizationalUnit -Filter "Name -like '*Workstations*'" | Select-Object Name, DistinguishedName
Run the script targeting a specific OU:
.\Export-BitLockerKeys.ps1 -SearchBase "OU=Workstations,OU=Computers,DC=yourdomain,DC=com" -OutputPath "C:\Reports\BitLocker_Workstations.csv"
You can also target multiple OUs by running the script multiple times with different SearchBase parameters:
# Export from Workstations OU
.\Export-BitLockerKeys.ps1 -SearchBase "OU=Workstations,DC=yourdomain,DC=com" -OutputPath "C:\Reports\BitLocker_Workstations.csv"
# Export from Laptops OU
.\Export-BitLockerKeys.ps1 -SearchBase "OU=Laptops,DC=yourdomain,DC=com" -OutputPath "C:\Reports\BitLocker_Laptops.csv"
Verification: Compare the number of results between domain-wide and OU-specific exports:
$workstationsCount = (Import-Csv "C:\Reports\BitLocker_Workstations.csv").Count
Write-Host "Workstations OU: $workstationsCount recovery keys found" -ForegroundColor Green
Pro tip: Use Get-ADComputer -Filter * -SearchBase "OU=YourOU,DC=domain,DC=com" | Measure-Object to get a quick count of computers in an OU before running the export.