Collect the list of users not found in AD

Hi All,
I have a CSV file that includes samaccountname’s of the users of our domain, the requirement is to fetch email address of these users. I have the script for that, but, there are few samaccountname’s whose object isn’t existing in AD, I have to collect and make a list of these, how do I do that? The thing is once, I collect them, I will have to either list them in mail bodies or attach the list onto a mail. Any help, would be really great!!!

PoorMansBravo,

Welcome to the forums.

It seems like you obviously already “recognise” them in any way with the code you’re using. Please show your code and the errors if there are some (both formatted as code please) and we can try to help you further.

1 Like

The script I am using is:
Get-Content c:\temp\userid.csv | foreach{Get-ADUser -Identity $_ -Properties *|select SamAccountName,EmailAddress,DisplayName}
The error which I see is:
Get-ADUser : Cannot find an object with identity: ‘usera’ under: ‘DC=Domain,DC=com’
My interest is to capture usera in the above error and have them send through an email

You should read up about error handling in PowerShell:

And please format your code as code her ein the forum ( simply use the </> icon in the post editor)

Hey Olaf,
Thanks for that guidance, I saw the Try,Catch,Finally thing. I understood, the Try, Catch part and prepared my script as seen below:
<try{
Get-Content c:\temp\userid.csv | foreach{Get-ADUser -Identity $_ -Properties *|select SamAccountName,EmailAddress,DisplayName}
}
catch{ “Get-ADUser : Cannot find an object with identity:”}
Finally{$Error}
/>
I am not sure whether this is correct because I am getting error

Get-ADUser : Cannot validate argument on parameter ‘Identity’. The Identity property on the argument is null or empty.
At line:2 char:62

  • … port-csv c:\temp\userid.csv | foreach{Get-ADUser -Identity $_ -Proper …

Could you please edit your message and format the code as code? Simply click on the </> icon and paste your code.

Thanks in advance.

1 Like
try{
Get-Content c:\temp\userid.csv | foreach{Get-ADUser -Identity $_ -Properties *|select SamAccountName,EmailAddress,DisplayName}
}
catch{ “Get-ADUser : Cannot find an object with identity:”}
Finally{$Error}```

Hi Olaf,
As you mentioned, have attached the code script.

That’s actually not quite what I meant but anyway. If you want to catch a single error with a single user you have make your error handling a little more precise.

$ADUserList =
Get-Content c:\temp\userid.csv |
ForEach-Object {
    try {
        $Identity = $_
        Get-ADUser -Identity $Identity -Properties EmailAddress, Mail -ErrorAction Stop | 
        Select-Object -Property SamAccountName, EmailAddress, Mail, DisplayName
    }
    catch {
        $NonADUserList += $Identity
    }
}

Now you have two variables available. One with the list of existing users and their email addresses and one with the non existing users.

$ADUserList
$NonADUserList

To make the try & catch block actually work you have to make sure you get terminating errors. If the cmdlet you’re about to use does not throw a terminating error by default you have to force it with the parameter -ErrorAction Stop

And just as an additional tip - with the cmdlet Get-ADUser you should only query the properties you’re actually interested in - not all available properties. That’s reducing the stress you put on your domain crontrollers. And it may speed up your code. :wink:

1 Like