Author Posts

January 11, 2017 at 3:06 pm

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'

January 11, 2017 at 3:14 pm

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.

January 11, 2017 at 3:45 pm

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

January 11, 2017 at 4:08 pm

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
}