Link to home
Start Free TrialLog in
Avatar of MilesLogan
MilesLoganFlag for United States of America

asked on

Check email address and if not correct , update correct email

Hi EE

I have a list of SamAccountnames and I need the script to check those accounts .
If the email address is not ABC.org , 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 anything.org , I need the script to change those to ABC.org

Can someone please assist with this .. or share an existing script
Avatar of Will Szymkowski
Will Szymkowski
Flag of Canada image

Try the following below...
                 
For this script to work you need to construct your csv like below...
samaccountname             emailaddress
dsmith                               dsmith.example.com
dbrown                              dbrown.example.com
etc...

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

Open in new window


Will.
Avatar of MilesLogan

ASKER

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 ..
ASKER CERTIFIED SOLUTION
Avatar of Will Szymkowski
Will Szymkowski
Flag of Canada image

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

Will.
Hi Will , my mistake .. I did not add the heading to the CSV file .. worked great ! thank you