Find if user is in AD by First and last Name

Welcome Forums General PowerShell Q&A Find if user is in AD by First and last Name

Viewing 13 reply threads
  • Author
    Posts
    • #238058
      Participant
      Topics: 3
      Replies: 14
      Points: 37
      Rank: Member

      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

    • #238070
      Participant
      Topics: 5
      Replies: 2373
      Points: 6,011
      Helping Hand
      Rank: Community MVP

      … 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. 😉

    • #238073
      Participant
      Topics: 12
      Replies: 1643
      Points: 2,665
      Helping Hand
      Rank: Community Hero

      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.

    • #238076
      Participant
      Topics: 3
      Replies: 14
      Points: 37
      Rank: Member

       

      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”
      }
       }
    • #238082
      Participant
      Topics: 12
      Replies: 1643
      Points: 2,665
      Helping Hand
      Rank: Community Hero

      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.

    • #238085
      Participant
      Topics: 3
      Replies: 14
      Points: 37
      Rank: Member
      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
    • #238091
      Participant
      Topics: 5
      Replies: 2373
      Points: 6,011
      Helping Hand
      Rank: Community MVP

      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.

    • #238094
      Participant
      Topics: 3
      Replies: 14
      Points: 37
      Rank: Member

      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

       

    • #238097
      Participant
      Topics: 3
      Replies: 14
      Points: 37
      Rank: Member

      Sorry very new to this

      Thanks

      Dan

    • #238100
      Participant
      Topics: 5
      Replies: 2373
      Points: 6,011
      Helping Hand
      Rank: Community MVP

      Sorry very new to this

      Please do not appologize – fix your posts!!

    • #238106
      Participant
      Topics: 3
      Replies: 14
      Points: 37
      Rank: Member

      Post Fixed

    • #238109
      Participant
      Topics: 3
      Replies: 14
      Points: 37
      Rank: Member
      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
    • #238118
      Participant
      Topics: 5
      Replies: 2373
      Points: 6,011
      Helping Hand
      Rank: Community MVP

      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' }
          }
      }
      
    • #238220
      Participant
      Topics: 3
      Replies: 14
      Points: 37
      Rank: Member

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

Viewing 13 reply threads
  • You must be logged in to reply to this topic.