Find if user is in AD by First and last Name

Hi All,

I have a huge CSV file with Firstname A and Lastname B

I want to import this and see if the user exists Within AD via Powershell, Looked around and tried to play around with some off the scripts online but I just get null values

Can This be done

Thanks

Dan

Please show us what you tried so far. Show your code.

This is absolutely feasible. And actually quite easy. :wink:

Yes, possible and requested often. The biggest issue is that an uncommon name is most likely a 1:1 match, but a common name like John Smith could have multiple matches, so you will need determine how you are going to manage 1:MANY matches. Share the code that you have tried.

 

Tried Using this, Was just getting no user in AD results back

PS C:\Temp> Import-Module ActiveDirectory get-content "c:\temp\Contact.txt" | foreach { $filter = [scriptblock]::create("DisplayName -like '*$_*'") if (Get-ADUser -Filter $filter){ Write-host "$_ does exist in AD"} else { Write-host "$_ does not exist in AD" }
}

Daniel, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
Thanks in advance.

Can you share an example of the CSV? Just a sample, 1 or 2 lines with fake data.

PS C:\Temp> Import-Module ActiveDirectory get-content "c:\temp\Contact.txt" | foreach { $filter = [scriptblock]::create("DisplayName -like '*$_*'") if (Get-ADUser -Filter $filter){ Write-host "$_ does exist in AD"} else { Write-host "$_ does not exist in AD" }
} Users does not exist in AD

Dan

Please read this carefully!!!

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.

Thanks for sharing on how to use the format advised

This is how I have my csv file

FirstName Lastname
Joe Blogs
Dan Burt
Ian Fowler
 

Sorry very new to this

Thanks

Dan

Please do not appologize - fix your posts!!

Post Fixed

Import-Module ActiveDirectory
$Users = Import-Csv -Path "Drive:\Folder\File.csv"

ForEach ($User in $Users) {
    $FirstName = $User.FirstName
    $LastName = $User.LastName
    Get-ADUser -Filter { (givenName -eq $FirstName) -and (surname -eq $LastName) }
}

I have this working to display the data I want to read the ID's which are not in AD and export them

Finally. Great. I’m proud of you.

If that’s all you need as info you could try something linke this:

Import-Csv -Path 'c:\temp\contact.csv' -Delimiter ',' |
ForEach-Object {
    [PSCustomobject]@{
        FirstName = $_.FirstName
        LastName  = $_.LastName
        ADAccount = if (Get-ADUser -Filter "GivenName -eq '$($_.FirstName)' -and SurName -eq '$($_.LastName)'" -ErrorAction SilentlyContinue) { 'cool' }else { 'uncool' }
    }
}

Thanks so Much for your help this is exactly how I wanted it