Link to home
Start Free TrialLog in
Avatar of abdull sheriff
abdull sheriff

asked on

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
Avatar of oBdA
oBdA

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

Avatar of abdull sheriff

ASKER

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

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

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

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

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

@Sheriff
I guess you have enough PS commands/scripts provided by the experts. Please test script/commands and award accordingly.
@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'
@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
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you