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

This topic contains 3 replies, has 4 voices, and was last updated by  Mark Prior 1 year, 3 months ago.

  • Author
  • #61704


    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

    Fredrik Kacsmarck

    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

    Rob Simmers

    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

    Mark Prior

    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

You must be logged in to reply to this topic.