POwershell script to pull email id's

I have a csv file with first name & last name of users in our domain.We need the email address of all users having the first name & last name.

I have tried quering using normal command only with first name but getting hell and lot of email addresses.

Can u please help in adding a condition to pull email address where first name & last name is equal.
LVL 2
AhmedAliShaikAsked:
Who is Participating?
 
Krzysztof PytkoConnect With a Mentor Senior Active Directory EngineerCommented:
That's strange but this code works fine for me. Copy code from a file and execute

code.txt

Krzysztof
0
 
AhmedAliShaikAuthor Commented:
I have an input file in the below format

FirstName LastName
a                   b
c                   d
e                   f

Can i get output in the below format.

FirstName LastName  emailaddress
a                   b                   emailadd of ab
c                   d                   emailadd of cd
e                   f                    emailadd of ef
0
 
Krzysztof PytkoSenior Active Directory EngineerCommented:
If you have Windows Server 2008R2 DC then run PowerShell and use below code

Import-Module ActiveDirectory
Import-CSV c:\users.CSV | %{
$fn=$_."FirstName"
$ln=$_."LastName"
Get-ADUser -Filter { (givenName -eq $fn) -and (sn -eq $ln) } -Properties * | Select givenName,sn,mail } | Export-CSV c:\fixedlist.CSV

Open in new window


Regards
Krzysztof
0
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
AhmedAliShaikAuthor Commented:
Hi Krzysztof,

Thanks you, i am running the script now.

Will update you once it is done.

Once again thanks a lot.
0
 
AhmedAliShaikAuthor Commented:
Hi Krzysztof,

Getting the below error:

Get-ADUser : Variable: 'fn' found in expression: $fn is not defined.
At C:\file.ps1:7 char:11
+ Get-ADUser <<<<  -Filter { (givenName -eq $fn) -and (sn -eq $ln) } -Properties * | Select givenName,sn,mail } | Expor
t-CSV E:\VPN\fixedlist.CSV
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : Variable: 'fn' found in expression: $fn is not defined.,Microsoft.ActiveDirectory.Manage
   ment.Commands.GetADUser.

Is it not importing firstname from csv file
0
 
Krzysztof PytkoSenior Active Directory EngineerCommented:
Copy whole code, paste into notepad and save as PS1 file
Execute it in PowerShell

Krzysztof
0
 
AhmedAliShaikAuthor Commented:
Still the same error.
:)
0
 
AhmedAliShaikAuthor Commented:
Hi ,

I have executed only the get-aduser with firstname and lastname but getting syntax error.

C:\get-aduser -Filter { (givenName -eq A) -and (sn -eq B) } -Properties * | Select givenName,sn,mail

Get-ADUser : Error parsing query: ' (givenName -eq A) -and (sn -eq B) ' Error Message: 'syntax error' at position: '17'.
At line:1 char:11
+ get-aduser <<<<  -Filter { (givenName -eq A) -and (sn -eq B) } -Properties * | Select givenName,sn,mail
    + CategoryInfo          : ParserError: (:) [Get-ADUser], ADFilterParsingException
    + FullyQualifiedErrorId : Error parsing query: ' (givenName -eq A) -and (sn -eq B) ' Error Message: 's
   yntax error' at position: '17'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser
0
 
Krzysztof PytkoSenior Active Directory EngineerCommented:
Ok, let me check that

Krzysztof
0
 
VishalnarseSystem Wintel Administrator L2Commented:
Hi,

Try this command on power shall

Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited -Filter {EmailAddresses -like "*@Domain.com.au"} | Forach-Object{
Set-Mailbox $_ -CustomAttribute5 "SYD" -EmailAddressPolicyEnabled $true -Whatif
$_ | Select-Object Name,Alias,WindowsEmailAddress
} | Export-Csv users.csv
0
 
AhmedAliShaikAuthor Commented:
Really its strange. Now the script is running without any errors.

Also i  can see the output csv file is increasing ...

:)

Hopefully i should get the expected output.

Will post you.

Thanks.
0
 
Krzysztof PytkoSenior Active Directory EngineerCommented:
Great, maybe () in syntax between attributes was not required and prevents code working? :) I have removed them from code.txt and probably it started working ;)

Krzysztof
0
 
VishalnarseSystem Wintel Administrator L2Commented:
0
 
AhmedAliShaikAuthor Commented:
Krzysztof.

Thanks a lot. Perfectly working.
0
 
Krzysztof PytkoSenior Active Directory EngineerCommented:
Great! I'm glad I could help

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

All Courses

From novice to tech pro — start learning today.