Adding Email address in bulk

Hi,

I’m new to Powershell and I’m trying to create a script to populate the E-mail field of a user in AD with their existing address, through csv file.

My csv looks like this:

samaccountname | emailaddress

Script I tried looks like this:

$users = Import-CSV C:\output\ADAddEmail.csv
Foreach($user in $users)
{

Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * |

Set-ADUser -emailaddress $user.emailaddress

}

I’m not sure why Powershell is telling me that the search filter cannot be recognized.
Anyone willing to assist?

Thanks in advance.

Hubert,
Welcome to the forum. :wave:t4:

If you already have sAMAccountNames in your CSV file you don’t need to search for the accounts with Get-ADUser at first.

It will be easier and faster when you directly provide the sAMAccountNames for the cmdlet Set-ADUser instead.

$userList = Import-CSV C:\output\ADAddEmail.csv
Foreach ($user in $userList) {
    Set-ADUser -Identity $($user.samaccountname) -EmailAddress $($user.emailaddress)
}

Regardless of that - when you get an error message and you’re about to ask for help about this error message you should provide this error message completely (formatted as code). :wink:

Thanks in advance.

Thanks for your reply!

And well noted. Just for completeness sake:

Get-ADUser : The search filter cannot be recognized
At C:\Powershell\Scripts\EmailAddressBulkAddAD.ps1:6 char:1
+ Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Pr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser

Hmmm … actually the syntax is correct. What delimitter do you use in your CSV file?

1 Like

Hmmm… you may be right. I’ll test it after my meeting and let you know. May have to add ; as a delimiter

Indeed, it was that simple. I think now I know why some other scripts are also not working.

Thanks very much for your input! I’ll try to be smarter in the future :slight_smile: