# Find if user is in AD by First and last Name

**URL:** <https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584>\
**Category:** PowerShell Help\
**Created:** [June 25, 2020, 3:08am UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584 "2020-06-25T03:08:41Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielburt123](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@danielburt123](https://forums.powershell.org/u/danielburt123)\
**Post date:** [June 25, 2020, 3:08am UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584/1 "2020-06-25T03:08:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 25, 2020, 3:45am UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584/2 "2020-06-25T03:45:34Z")

</div>

> [@](#):
>
> … Looked around and tried to play around with some off the scripts online but I just get null values

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

> [@](#):
>
> Can This be done

This is absolutely feasible. And actually quite easy. 😉

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [June 25, 2020, 3:49am UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584/3 "2020-06-25T03:49:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![danielburt123](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@danielburt123](https://forums.powershell.org/u/danielburt123)\
**Post date:** [June 25, 2020, 3:49am UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584/4 "2020-06-25T03:49:57Z")

</div>

&nbsp;

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" }

 }

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [June 25, 2020, 3:57am UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584/5 "2020-06-25T03:57:32Z")

</div>

Daniel, welcome to [Powershell.org](http://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!](https://powershell.org/forums/topic/read-me-before-posting-youll-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.

---

<div class="post-metadata">

**Author:** ![danielburt123](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@danielburt123](https://forums.powershell.org/u/danielburt123)\
**Post date:** [June 25, 2020, 4:00am UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584/6 "2020-06-25T04:00:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 25, 2020, 4:02am UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584/7 "2020-06-25T04:02:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![danielburt123](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@danielburt123](https://forums.powershell.org/u/danielburt123)\
**Post date:** [June 25, 2020, 4:03am UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584/8 "2020-06-25T04:03:54Z")

</div>

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 |
| | |
| | |

&nbsp;

---

<div class="post-metadata">

**Author:** ![danielburt123](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@danielburt123](https://forums.powershell.org/u/danielburt123)\
**Post date:** [June 25, 2020, 4:05am UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584/9 "2020-06-25T04:05:13Z")

</div>

Sorry very new to this

Thanks

Dan

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 25, 2020, 4:06am UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584/10 "2020-06-25T04:06:33Z")

</div>

> [@](#):
>
> Sorry very new to this

Please do not appologize - fix your posts!!

---

<div class="post-metadata">

**Author:** ![danielburt123](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@danielburt123](https://forums.powershell.org/u/danielburt123)\
**Post date:** [June 25, 2020, 4:20am UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584/11 "2020-06-25T04:20:43Z")

</div>

Post Fixed

---

<div class="post-metadata">

**Author:** ![danielburt123](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@danielburt123](https://forums.powershell.org/u/danielburt123)\
**Post date:** [June 25, 2020, 4:47am UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584/12 "2020-06-25T04:47:24Z")

</div>

```
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
```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 25, 2020, 5:40am UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584/13 "2020-06-25T05:40:51Z")

</div>

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' }
    }
}
```

---

<div class="post-metadata">

**Author:** ![danielburt123](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@danielburt123](https://forums.powershell.org/u/danielburt123)\
**Post date:** [June 25, 2020, 12:06pm UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584/14 "2020-06-25T12:06:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/find-if-user-is-in-ad-by-first-and-last-name/14584/15 "2024-05-16T20:29:18Z")

</div>


