Comparing Email List to AD

This is my current code which isn't doing what I want.

$UserList = get-content C:\Users\*****\Desktop\UserList\UserList.csv
$yesEmail = @()
$allUsers = Get-ADUser -Filter * -Properties `
GivenName,Surname,SamAccountName,Name,EmailAddress | ?{ $_.Enabled -eq $true } | `
Select GivenName,Surname,SamAccountName,Name,EmailAddress

    foreach ($u in $allUsers){
        if ($UserList -notcontains $u.EmailAddress){
            $yesEmail += New-Object PSObject -Property @{
                GivenName = $u.GivenName
                Surname = $u.Surname
                DisplayName = $u.Name
                samAccountName = $u.SamAccountName
                EmailAddress = $u.EmailAddress
                }
            }
        }
$yesEmail | Select GivenName,Surname,SamAccountName,Name,EmailAddress | `
Export-Csv C:\Users\*****\Desktop\UserList\Difference.csv

UserList.csv is a list of email addresses.
I want to compare them to all users in AD and compile of list of users that aren't in UserList.

Thanks for any help.
-Judical

August 21st, 2015 4:11pm

On $userlist you could try import-csv instead of get-content. And maby try -notmatch instead of -notcontains.

 Edit: jrv is correct, I missed reading the 2 last sentences.

Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 5:10pm

On $userlist you could try import-csv instead of get-content. And maby try -notmatch instead of -notcontains.

 Edit: jrv is correct, I missed reading the 2 last sentences.

August 21st, 2015 5:17pm

In this case "notcontains" is correct.

Is "userlist" a single column of email addresses?

If the above is true try this and post the exact errors.

$UserList=Get-Content C:\Users\*****\Desktop\UserList\UserList.csv
Get-ADUser -Filter {Enabled -eq $true} -Properties GivenName,Surname,SamAccountName,Name,EmailAddress|
	Where-Object{$UserList -notcontains $_.EmailAddress}|
 	select GivenName,Surname,SamAccountName,Name,EmailAddress |
 	Export-Csv C:\Users\*****\Desktop\UserList\Difference.csv 

Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 5:37pm

On $userlist you could try import-csv instead of get-content. And maby try -notmatch instead of -notcontains.

 Edit: jrv is correct, I missed reading the 2 last sentences.

August 21st, 2015 9:05pm

This works, thank you very much.
Not being a guru could you tell how I could use a document that has many columns one of which being email addresses?

-Judical

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 8:10am

Use Import-Csv instead of Get-Content.

http://ss64.com/ps/import-csv.html

August 24th, 2015 8:21am

Would it look like this?

$UserList=(Import-Csv "C:\Users\****\Desktop\UserList\UserReport.csv")."Email Address"

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 10:35am

Sure.
August 24th, 2015 10:38am

More help if you don't mind.
I am now trying to filter out results that do not have an employee number.

This is my current script;

$UserList=(Import-Csv "C:\Users\****\Desktop\UserList\UserReport.csv")."Employee Number"
Get-ADUser -Filter {Enabled -eq $true} -Properties EmployeeNumber,SamAccountName,Name,EmailAddress|
Where-Object{$UserList -notcontains $_.EmployeeNumber}|
  select EmployeeNumber,SamAccountName,Name,EmailAddress |
  Export-Csv C:\Users\****\Desktop\UserList\DifferenceNEW.csv

I assume the code i'm looking for is this | where {$_."EmployeeNumber" -ne ""}
However I am not sure where how or where to add it to the script.

-Judical

Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 11:30am

More help if you don't mind.
I am now trying to filter out results that do not have an employee number.

This is my current script;

$UserList=(Import-Csv "C:\Users\****\Desktop\UserList\UserReport.csv")."Employee Number"
Get-ADUser -Filter {Enabled -eq $true} -Properties EmployeeNumber,SamAccountName,Name,EmailAddress|
Where-Object{$UserList -notcontains $_.EmployeeNumber}|
  select EmployeeNumber,SamAccountName,Name,EmailAddress |
  Export-Csv C:\Users\****\Desktop\UserList\DifferenceNEW.csv

I assume the code i'm looking for is this | where {$_."EmployeeNumber" -ne ""}
However I am not sure where how or where to add it to the script.

-Judical

get-aduser -properties employeenumber -Filter {employeenumber -notlike "*"}
August 24th, 2015 1:39pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics