Powershell Query AD disabled Accounts compare with Email Address - JoelRochambeau/Powershell GitHub Wiki
#Query AD and get all Disabled accounts, select email address only
$Users = Get-ADUser -Filter 'enabled -eq $true' -SearchBase "DC=dhs,DC=sdc,DC=pvt" -Properties mail | Select-Object -ExpandProperty mail
#Import the extracted report from Alexsys, this is my location i saved MIM report
$SysUsers = Get-Content 'E:\Working\Scripts\Active Directory\Compare to Notepad Enabled\UserEmailFile.txt'
#create and initialize array the foreach created and populates the array based on the Query AD user details
$TargetArray= @()
$AllUsersEnabledinBoth = @()
Foreach ($user in $Users)
{
#Put each disabled user from the AD query above ($Users) into an array
$TargetProperties = @{Name=$User}
$TargetObject = New-Object PSObject –Property $TargetProperties
$TargetArray += $TargetObject
}
foreach ($User in $SysUsers){ #First loop to select each individual user in file then we need to run a comparison loop to compare it to AD disabled accounts Foreach ($ADEmail in $TargetArray){ #Validate and compare the file email address to the AD Email address if ($User -eq $ADEmail.name){ #If valid email match then check to see if its a licensed user #Print details (this can be saved as file, or i can have the system email us) write-host "This User exists in AD and is Enabled : $user" $AllUsersEnabledinBoth += $user}
}
}