Check email address and if not correct , update correct email


I have a list of SamAccountnames and I need the script to check those accounts .
If the email address is not , I need it to correct it and replace what ever is there with ABC.ORG

So lets say , the script finds 5 accounts that have , I need the script to change those to

Can someone please assist with this .. or share an existing script
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Will SzymkowskiSenior Solution ArchitectCommented:
Try the following below...
For this script to work you need to construct your csv like below...
samaccountname             emailaddress

Import-module activedirectory
$users = import-csv "c:filename.csv" | Get-ADUser -Properties mail
ForEach ($user in $users) {
If ($user.mail -ne $user.emailaddress) {
    Set-ADUser -Identity $user -Mail $user.emailaddress

Open in new window

MilesLoganAuthor Commented:
Hi Will , I am receiving the error below.

Get-ADUser : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At C:\PS\CheckEmail.ps1:2 char:63
+ $users = import-csv “C:\PS\CheckEmailUsers.csv" | Get-ADUser -Proper ..
Will SzymkowskiSenior Solution ArchitectCommented:
Sorry i was just typing this out and i did not notice Get-ADUser does not accept pipeline info.

I have tested this script out and i have modified this slightly. This should work for you. Make sure that you keep the same CSV outline.

import-module activedirectory
$Users = import-csv "c:\filename.csv"
ForEach ($User in $Users) {
$AccountCheck = Get-ADUser -Identity $User.samaccountname -Properties emailaddress | select emailaddress
     if ($AccountCheck -ne $User.EmailAddress) {
         Set-ADUser -Identity $User.samaccountname -EmailAddress $User.emailaddress

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SD-WAN: Making It Work for You

As bandwidth requirements and Internet costs grow, businesses naturally want to manage budgets by reducing reliance on their most expensive connection types. Learn more about how to make SD-WAN work for your business in our on-demand webinar!

MilesLoganAuthor Commented:
Hi Will .. I am now receiving a different error ..

Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
At C:\PS\CheckEmail.ps1:6 char:38
+ $AccountCheck = Get-ADUser -Identity $User.samaccountname -Properties emailaddre ...
Will SzymkowskiSenior Solution ArchitectCommented:
I have checked this again and the script works fine. The error you are receiving is due to how you have your CSV file constructed. Do you have samaccountname and emailaddress as column headings in your CSV file?

Also the list of samaccountname you have in your CSV do they actually exist in Active Directory?

Double check everything as i have tested this in 2 different environment and had no issues with my most recent script i posted.

MilesLoganAuthor Commented:
Hi Will , my mistake .. I did not add the heading to the CSV file .. worked great ! thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.