Try this one:
$OUNames = "OU=FMI,OU=Cognizant,OU=FM Users,OU=Corp,DC=test-tech,DC=com", "OU=BPO and RPA,OU=Cognizant,OU=Consultants,OU=Users,OU=Corp,DC=test-tech,DC=com"
# Load hash with UPNs
$UPNs = @{}
$OUNames |
ForEach-Object{
Get-ADUser -Filter * -SearchBase $_ -SearchScope OneLevel |
ForEach-Object{
$UPNs[$_.UserPrincipalName] = $false
}
}
Import-Csv C:\temp\test2.csv |
ForEach-Object {
$u = get-aduser -Filter "userPrincipalName -eq '$($_.upn)'"
if ($u) {
Set-ADAccountExpiration -Identity $u.distinguishedName -TimeSpan 90.0:0
$UPNs.($_.UPN) = $true
} else {
[PSCustomObject]@{
UPN = $_.UPN
Reason = "UPN not found in AD"
}
}
} | Export-CSV C:Temp\NotInAD.csv -NoTypeInformation
$UPNs.GetEnumerator()|
ForEach-Object{
if (-not $_.Value){
[PSCustomObject]@{
UPN = $_.Key
Reason = "UPN not in CSV, or UPN in different OU"
}
}
} | Export-Csv C:\Temp\NotInCSV.csv -NoTypeInformation