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
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'
From Exchange server
From domain controller you can try this
Get-Mailbox -Database "database" -ResultSize unlimited | select name, displayname, alias, PrimarySmtpAddress, database | Export-Csv c:\temp\results.csv
From domain controller you can try this
Get-Recipient | select primarysmtpaddress,firstname,lastname,alias,displayname | Export-Csv c:\temp\mailboxdetails.csv
ASKER
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
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'
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
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'
@Sheriff
I guess you have enough PS commands/scripts provided by the experts. Please test script/commands and award accordingly.
I guess you have enough PS commands/scripts provided by the experts. Please test script/commands and award accordingly.
ASKER
@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'
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'
ASKER
@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-Ma ilbox -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], ManagementObjectNotFoundEx ception
+ FullyQualifiedErrorId : 355BBD08,Microsoft.Exchang e.Manageme nt.Recipie ntTasks.Ge tMailbox
When running from the Domain Controller i get the below error
PS C:\Windows\system32> Get-Recipient | select primarysmtpaddress,firstna me,lastnam e,alias,di splayname | 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,firstna me,lastnam e,alias,di splayname | ...
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-Recipient:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
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-Ma
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], ManagementObjectNotFoundEx
+ FullyQualifiedErrorId : 355BBD08,Microsoft.Exchang
When running from the Domain Controller i get the below error
PS C:\Windows\system32> Get-Recipient | select primarysmtpaddress,firstna
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,firstna
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-Recipient:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you
Open in new window