question

RenuMurali-7202 avatar image
0 Votes"
RenuMurali-7202 asked RichMatheisen-8856 answered

Powershell script to compare .CSV file to AD. Export all the active and inactive emails in seperate .csv files.

I have a .csv file with the column names "Display Name, Email Address and SameAccountName". I want to compare this .csv file to active directory. So essentially, if any emails under "EmailAddress" from the .csv file does not match the "EmailAddress" from AD, I want to export the names that does not match into a separate .csv file named "inactiveusers.csv".
My script :

 $csv=Import-Csv -Path C:\Users\renu\Desktop\renu\ALLUserMain30082021.csv
 $ad = Get-ADUser -Filter "Enabled -eq 'True'" -SearchBase 'OU=example,DC=example,DC=example,DC=example'
 Compare-Object -ReferenceObject $csv.EmailAddress -DifferenceObject $ad.EmailAddress -IncludeEqual -ExcludeDifferent | Select-Object -Property InputObject | Export-Csv -Path C:\Users\renu\Desktop\renu\match.csv -NoTypeInformation
 Compare-Object -ReferenceObject $csv.EmailAddress -DifferenceObject $ad.EmailAddress | Select-Object -Property InputObject | Export-Csv -Path C:\Users\renu\Desktop\renu\notmatchusers.csv -NoTypeInformation

When i run the code i get the following error message.127568-error.png


windows-server-powershell
error.png (50.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

I don't see why you're encountering that error unless there are no enabled users in the organizational unit.

Run this to verify that you get the expected results:

 $c = @"
 EmailAddress
 a@z.com
 b@z.com
 d@z.com
 "@
 $csv = $c | ConvertFrom-Csv
    
 $a = @"
 Name,EmailAddress
 Adam,a@z.com
 Beth,b@z.com
 Carol,c@z.com
 Doug,d@z.com
 "@
 $ad = $a | ConvertFrom-Csv
    
 Compare-Object -ReferenceObject $csv.EmailAddress -DifferenceObject $ad.EmailAddress -IncludeEqual -ExcludeDifferent |
     Select-Object -Property InputObject |
         Export-Csv -Path C:\junk\match.csv -NoTypeInformation
    
 Compare-Object -ReferenceObject $csv.EmailAddress -DifferenceObject $ad.EmailAddress |
     Select-Object -Property InputObject |
         Export-Csv -Path C:\junk\notmatchusers.csv -NoTypeInformation
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.