• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 32
  • Last Modified:

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
0
abdull sheriff
Asked:
abdull sheriff
  • 5
  • 4
  • 4
  • +1
1 Solution
 
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
MAS (MVE)Technical Department 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
 
MAS (MVE)Technical Department 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
 
MAS (MVE)Technical Department 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
 
MAS (MVE)Technical Department 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
 
abdull sheriffSystems EngineerAuthor Commented:
thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

WEBINAR: 10 Easy Ways to Lose a Password

Join us on June 27th at 8 am PDT to learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees. We'll cover the importance of multi-factor authentication and how these solutions can better protect your business!

  • 5
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now