Check for Active Directory Accounts PowerShell Script


Today I had a few spare minutes to cleanup our inactive users in our Adobe admin console. I discovered that we had users still active that were no longer active in our Active Directory. Adobe provides a mechanism to delete users by CSV and then for the admin to modify the CSV and then upload it back to the Adobe Admin Console to delete the users in the CSV. The tricky part is when you are dealing with 62,000 accounts looking through all of them would be ridiculous; PowerShell was able to save the day.

Here is the script I used to check for active directory accounts existence and if not existing to export to a CSV usable to upload to the Adobe Admin Console to delete. I did not worry about enabled vs disabled accounts at this time as there were many that simply did not exist that needed cleanup first.

In the script set $CSV to the CSV file downloaded from Adobe or your input list and then $MissingUsersCSV to the output path.

$CSV = "$env:userprofile\downloads\users.csv"
$MissingUsersCSV = "$env:userprofile\downloads\deletedusers.csv"
$MissingUsers = @()

Import-csv $CSV | ForEach-Object {

    $Username = $_.Username
    $Email = $_.Email
    
    If (Get-ADUser -Filter {Emailaddress -eq $Email}) {
        Write-Output "$username found"
        Write-Output " "
        }
    Else {
        Write-Warning "$Username not found in AD"
        write-output " "
        $MissingUsers = $MissingUsers + $_
        }
}

$MissingUsers | Export-Csv $MissingUsersCSV -NoTypeInformation

I hope you find this as useful as I did. There are many other ways to solve this same problem, but this was what was quickest and easiest to me. Happy scripting!


Leave a Reply

Your email address will not be published. Required fields are marked *