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

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

This topic contains 3 replies, has 4 voices, and was last updated by

 
Participant
2 years, 5 months ago.

  • Author
    Posts
  • #61704

    Participant
    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

    Participant
    Topics: 12
    Replies: 218
    Points: 398
    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

    Participant
    Topics: 8
    Replies: 1191
    Points: 644
    Helping Hand
    Rank: Major Contributor

    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

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

The topic ‘Extract Information (Name, Mobile, etc.) from AD’ is closed to new replies.