First Name, Last Name and display name for all the users

i need to get First Name, Last Name and display name for all the users in my AD using power shell script in a csv file
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.

oBdACommented:
Import-Module ActiveDirectory
Get-ADUser -Filter * -Property DisplayName, GivenName, SurName |
	Select-Object -Property SamAccountName, DisplayName, @{n='FirstName'; e={$_.GivenName}}, @{n='LastName'; e={$_.SurName}} |
	Sort-Object -Property SamAccountName |
	Export-Csv -NoTypeInformation -Path 'C:\Temp\ADUsers.csv'

Open in new window

1
abdull sheriffSystems EngineerAuthor Commented:
great, it worked, now i have a csv file with just selected user's first and last name i need their email address in the output file along with first and last name
0
oBdACommented:
Just add it to the properties in lines 2 and 3:
Import-Module ActiveDirectory
Get-ADUser -Filter * -Property DisplayName, GivenName, SurName, mail |
	Select-Object -Property SamAccountName, DisplayName, @{n='FirstName'; e={$_.GivenName}}, @{n='LastName'; e={$_.SurName}}, Mail |
	Sort-Object -Property SamAccountName |
	Export-Csv -NoTypeInformation -Path 'C:\Temp\ADUsers.csv'

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

MASEE Solution Guide - Technical Dept HeadCommented:
From Exchange server
Get-Mailbox -Database "database" -ResultSize unlimited | select name, displayname, alias, PrimarySmtpAddress, database | Export-Csv c:\temp\results.csv

Open in new window


From domain controller you can try this
Get-Recipient | select primarysmtpaddress,firstname,lastname,alias,displayname | Export-Csv c:\temp\mailboxdetails.csv 

Open in new window

0
abdull sheriffSystems EngineerAuthor Commented:
no i have a csv file (with first name and last name) and i need to import only those users to get the info (email address) and not for all the users in AD
0
MASEE Solution Guide - Technical Dept HeadCommented:
Please try this command from Exchange powersehll. Your CSV should be similar to the attached CSV
Import-Csv C:\cert\mailboxdetails.csv | ForEach-Object{Get-recipient -Filter "FirstName -eq '$($_.FirstName)' -and LastName -eq '$($_.LastName)'" } | Select-Object FirstName, LastName, PrimarySmtpAddress

Open in new window

mailboxdetails.csv
0
oBdACommented:
Assuming an input file with (at least) two columns FirstName, LastName:
Import-Module ActiveDirectory
Import-Csv -Path C:\Temp\UserFirstLast.csv | ForEach-Object {
	If ($ADUsers = Get-ADUser -Filter "(givenName -eq '$($_.FirstName)') -and (surName -eq '$($_.LastName)')" -Property GivenName, SurName, mail) {
		$ADUsers
	} Else {
		$_ | Select-Object -Property @{n='SamAccountName'; e={'<NOT_FOUND>'}}, @{n='GivenName'; e={$_.FirstName}}, @{n='SurName'; e={$_.LastName}}
	}
} | Select-Object -Property SamAccountName, @{n='FirstName'; e={$_.GivenName}}, @{n='LastName'; e={$_.SurName}}, Mail |
	Export-Csv -NoTypeInformation -Path 'C:\Temp\ADUsers.csv'

Open in new window

0
MASEE Solution Guide - Technical Dept HeadCommented:
Missed to add export-csv in the command
Import-Csv C:\cert\mailboxdetails.csv | ForEach-Object{Get-recipient -Filter "FirstName -eq '$($_.FirstName)' -and LastName -eq '$($_.LastName)'" } | Select-Object FirstName, LastName, PrimarySmtpAddress | Export-Csv c:\maildetails.csv

Open in new window

0
MaheshArchitectCommented:
Try below

Get-ADUser -Filter * -Property DisplayName, GivenName, SurName, Proxyaddesses | Select SamAccountName, DisplayName, givenname, surname -Expandproperty @(ProxyAddresses | ? {$_ -clike "SMTP:*"}) | Export-Csv -NoTypeInformation -Path 'C:\Temp\ADUsers.csv'

Open in new window

1
MASEE Solution Guide - Technical Dept HeadCommented:
@Sheriff
I guess you have enough PS commands/scripts provided by the experts. Please test script/commands and award accordingly.
0
abdull sheriffSystems EngineerAuthor Commented:
@oBda

Your last script got executed and displayed SamAccountName, FirstName, LastName and Email address and when i add 2 more properties like DisplayName and UserPrincipalName, i dont get any info under DisplayName but i get for UserPrincipalName and below is the script i ran

Import-Module ActiveDirectory
Import-Csv -Path C:\flIN.csv | ForEach-Object {
If ($ADUsers = Get-ADUser -Filter "(givenName -eq '$($_.FirstName)') -and (surName -eq '$($_.LastName)')" -Property GivenName, SurName, mail) {
$ADUsers
} Else {
$_ | Select-Object -Property @{n='SamAccountName'; e={'<NOT_FOUND>'}}, @{n='GivenName'; e={$_.FirstName}}, @{n='SurName'; e={$_.LastName}}
}
} | Select-Object -Property SamAccountName, @{n='FirstName'; e={$_.GivenName}}, @{n='LastName'; e={$_.SurName}}, Mail, DisplayName, UserPrincipalName |
Export-Csv -NoTypeInformation -Path 'C:\flOUT.csv'
0
abdull sheriffSystems EngineerAuthor Commented:
@MAS

I get errors when running your commands respectively from exchange and Domain Controller

When running from exchange i get the below error

[PS] C:\Windows\system32>Get-Mailbox -Database "database" -ResultSize unlimited | select name, displayname, alias, Prima
rySmtpAddress, database | Export-Csv c:\temp\results.csv
Couldn't find database "database". Make sure you have typed it correctly.
    + CategoryInfo          : NotSpecified: (0:Int32) [Get-Mailbox], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : 355BBD08,Microsoft.Exchange.Management.RecipientTasks.GetMailbox

When running from the Domain Controller i get the below error

PS C:\Windows\system32> Get-Recipient | select primarysmtpaddress,firstname,lastname,alias,displayname | Export-Csv c:\mailboxdetails.csv
Get-Recipient : The term 'Get-Recipient' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At line:1 char:1
+ Get-Recipient | select primarysmtpaddress,firstname,lastname,alias,displayname | ...
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-Recipient:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
0
oBdACommented:
You get the UserPrincipalName because it's part of the default set of attributes that will be returned (like SamAccountName).
When you add properties, you need to do so in line 3 and line 8. The order in line 8 doesn't matter, the order in line 8 defines the order in which the columns appear in the csv.
Import-Module ActiveDirectory
Import-Csv -Path C:\Temp\UserFirstLast.csv | ForEach-Object {
	If ($ADUsers = Get-ADUser -Filter "(givenName -eq '$($_.FirstName)') -and (surName -eq '$($_.LastName)')" -Property GivenName, SurName, mail, DisplayName) {
		$ADUsers
	} Else {
		$_ | Select-Object -Property @{n='SamAccountName'; e={'<NOT_FOUND>'}}, @{n='GivenName'; e={$_.FirstName}}, @{n='SurName'; e={$_.LastName}}
	}
} | Select-Object -Property SamAccountName, @{n='FirstName'; e={$_.GivenName}}, @{n='LastName'; e={$_.SurName}}, Mail, DisplayName, UserPrincipalName |
	Export-Csv -NoTypeInformation -Path 'C:\Temp\ADUsers.csv'

Open in new window

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
abdull sheriffSystems EngineerAuthor Commented:
thank you
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.