Extract Information (Name, Mobile, etc.) from AD

Welcome Forums General PowerShell Q&A Extract Information (Name, Mobile, etc.) from AD

Viewing 3 reply threads
  • Author
    • #61704
      Topics: 2
      Replies: 2
      Points: 0
      Rank: Member

      Hey guys,
      I was trying to create a script where I want to extract DisplayName, Mobile number, etc. of the employees in our company from a txt file that was provided to me with Usernames or Network ID only of them employees. I tried the below script and it did exported the information I needed but instead it exported everyone in AD. SO basically I needed only at least 50-60 people (saved in the txt file) but the .csv file contained the entire directory with around 15,000 of our employees, so the below script might be missing something to filter the txt file. Anything I missed?

      $users = ForEach ($user in $(get-content ‘C:\Users\username\Desktop\SMS Blast.txt’)) {

      Get-AdUser -Filter * -Properties Mobile, DisplayName


      $users |
      Select-Object SamAccountName,Mobile,DisplayName |
      Export-CSV -Path ‘C:\Users\signacio\Desktop\SMS Blast.csv’

    • #61705
      Topics: 12
      Replies: 232
      Points: 466
      Helping Hand
      Rank: Contributor

      You would need to change the Get-ADUser command to something like.

      Get-ADUser -Identity $user -Properties Mobile,DisplayName

      You’re are calling for all users with “-Filter *” every time you get to the next line in the text file.

    • #61708
      Topics: 12
      Replies: 1642
      Points: 2,660
      Helping Hand
      Rank: Community Hero

      Side note. Get-ADUser -Identity will need a try\catch because an non-existing user will generate a exception. Another option:

      $user = Get-ADUser -Filter {SamAccountName -eq $user} -Properties Mobile,DisplayName
      if ($user) {
          #User exists, get properties
          $user | Select-Object SamAccountName,Mobile,DisplayName
      else {
          #User does not exist, create PSObject
          New-Object -TypeName PSObject -Property @{
              SamAccountName = $user
              Mobile         = $null
              DisplayName    = USER_NOT_FOUND
    • #61713
      Topics: 47
      Replies: 156
      Points: 42
      Rank: Member

      without seeing your text file its hard to tell but below should work (if not remove the $_.)

      $users = get-content "C:\Users\username\Desktop\SMS Blast.txt"
      foreach ($user in $users)
      Get-ADUser -Identity $_.user -Properties Mobile,DisplayName
Viewing 3 reply threads
  • The topic ‘Extract Information (Name, Mobile, etc.) from AD’ is closed to new replies.