• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1160
  • Last Modified:

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.
0
AhmedAliShaik
Asked:
AhmedAliShaik
  • 7
  • 6
  • 2
1 Solution
 
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 PytkoActive 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
 
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 PytkoActive 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 PytkoActive Directory EngineerCommented:
Ok, let me check that

Krzysztof
0
 
Krzysztof PytkoActive Directory EngineerCommented:
That's strange but this code works fine for me. Copy code from a file and execute

code.txt

Krzysztof
0
 
VishalnarseCommented:
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 PytkoActive 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
 
VishalnarseCommented:
0
 
AhmedAliShaikAuthor Commented:
Krzysztof.

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

Krzysztof
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 7
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now