export list from exchange for email id and phone numbers

I need to get my AD user's primary email id and phone numbers, the issue i am facing here is that i dont have the display name i just have their first and last name, so when i run ps script with the first and last name in exchange powershell it thorws an error that the object cannot be found and it works when i give their display name.

Any idea how can i find their email id and phone numbers just with their first and last name
i have a list for 1000 users
abdull sheriffSystems EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alex GreenProject Systems EngineerCommented:
Consolidate their first and last name into a single text file.

$Users =  import-content "c:\temp\users.txt
Foreach ($user in $Users) {Get-ADUser -Filter { displayName -like "*$User*" } | Select-object Samaccountname, email, telephoneNumber| Expoprt-csv C:\temp\results.csv}
1
abdull sheriffSystems EngineerAuthor Commented:
[PS] C:\Windows\system32>$Users =  import-content "c:\user1.txt" Foreach ($user in $Users) {Get-ADUser -Filter { display
Name -like "*$User*" } | Select-object Samaccountname, email, telephoneNumber| Expoprt-csv C:\results.csv}
Unexpected token 'in' in expression or statement.
At line:1 char:58
+ $Users =  import-content "c:\user1.txt" Foreach ($user in <<<<  $Users) {Get-ADUser -Filter { displayName -like "*$Us
er*" } | Select-object Samaccountname, email, telephoneNumber| Expoprt-csv C:\results.csv}
    + CategoryInfo          : ParserError: (in:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

when i run i get this error and i am running this in exchange powershell and i have my user list txt file under c
0
Alex GreenProject Systems EngineerCommented:
$Users = import-content "c:\temp\users.txt"
Foreach ($user in $Users) {Get-ADUser -Filter { displayName -like "*$User*" } | Select-object Samaccountname, email, telephoneNumber| Expoprt-csv C:\temp\results.csv}


Missing closing "
0
Redefine Your Security with AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Check out our on-demand webinar to learn more about how AI can help your organization!

abdull sheriffSystems EngineerAuthor Commented:
i included the " when i run the command , you can see it in my output
0
Alex GreenProject Systems EngineerCommented:
$Users = get-content "c:\temp\users.txt"
Foreach ($user in $Users) {Get-ADUser -Filter { displayName -like "*$User*" } -properties * | Select-object Samaccountname, email, telephoneNumber| Export-csv C:\temp\results.csv}
0
abdull sheriffSystems EngineerAuthor Commented:
it ran this time but the file is with 0kb, its blank
0
Alex GreenProject Systems EngineerCommented:
$Users = get-content "c:\temp\users.txt"
Foreach ($user in $Users) {Get-ADUser -LDAPFilter "(Displayname=$User)" -properties * | Get-ADUser -Properties * | Select-Object Name, Samaccountname, mail, OfficePhone | export-csv C:\temp\results.csv -append}


Apparenlty it doesn't like searching on a displayname. This does 100 work
0
abdull sheriffSystems EngineerAuthor Commented:
ok this works when the txt file have users with display name in it

thanks for helping me out here , let me rephrase my issue again

for instance :
the script works when the txt file have a user like Abdull Sheriff Accounts Manager
the script does not works when the txt file have a user like Abdull Sheriff

My problem here i have a list of users with their first and last name only and i dont have their display name

First Name: Abdull
Last Name: Sheriff
Display Name : Abdull Sheriff Accounts Manager
0
abdull sheriffSystems EngineerAuthor Commented:
i am okay to have a script where i can first get their the display name with first or last name and then run the script with the display name in the txt file
0
Alex GreenProject Systems EngineerCommented:
Urgh, this is a shocking way to set up AD...

Fine,

$names = Import-CSV .\users.csv -Header Givenname,Surname -Delimiter ";"
ForEach ($Name in $Names)
{
    $FirstFilter = $Name.Givenname
    $SecondFilter = $Name.Surname
    Get-ADUser -Filter { GivenName -like $FirstFilter -and Surname -like $SecondFilter} |select enabled,samaccountname,@{n="ou";e={($_.distinguishedname -split ",*..=")[2]}}
}

That'll give you the samaccounnnames

Then do

$Users = get-content "c:\temp\users.txt"
Foreach ($user in $Users) {Get-ADUser -identity $user -properties * | Select-object Samaccountname, email, telephoneNumber| Export-csv C:\temp\results.csv}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Aaron GuilmetteTechnology Solutions ProfessionalCommented:
Why not just get *everyone's* data and then sort through it with Excel or use further PowerShell to limit it?

Get-Aduser -Filter * | ? { $_.DisplayName -ne $null } | Select DisplayName,givenName,sn,telephoneNumber

or something similar?
1
Mohammad Ishtyaq khatriSr. Systems EngineerCommented:
As mentioned by @Aaron Guilmette do a All users and then do a Vlookup in Excel to match the first name last name from the list you have.

You should get what you need ;)
0
abdull sheriffSystems EngineerAuthor Commented:
When running the below i get all the information except for mobile,

Import-Module activedirectory
$path = "c:\ADUsers123.csv"
Get-ADUser -Filter * |
Select-Object name, enabled, Mobile, userprincipalname, Samaccountname, email, telephoneNumber | Export-Csv -Path $path -NoTypeInformation

i tried mobile, telephoneNumber still the same, any wrong in the script???
0
abdull sheriffSystems EngineerAuthor Commented:
thanks all
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.