Solved

Powershell: Update email for multiple users in Active Directory

Posted on 2014-01-24
11
3,207 Views
Last Modified: 2014-01-24
Hi Guys

I need to update the email address for multiple AD accounts .. I tried below but I am missing something .. help  ?

Import-module ActiveDirectory
Import-CSV "E:\Projects\Users\EmailUsers.csv" | % {
Set-ADUser $_.User -EmailAddress $_.EmailAddress
}


EmailUsers.csv file format I have
SamAccountName,Email



Error:
Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
At E:\Projects\Users\UpdateEmail.ps1:3 char:12
+ Set-ADUser $_.User -EmailAddress $_.EmailAddress
+            ~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser
0
Comment
Question by:MilesLogan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 39807985
get-aduser -identity $_.SamAccountName | Set-ADUser $_.User -EmailAddress $_.EmailAddress

Import-module ActiveDirectory
Import-CSV "E:\Projects\Users\EmailUsers.csv" | % {
get-aduser -identity $_.SamAccountName | Set-ADUser -EmailAddress $_.EmailAddress
}

Open in new window

0
 
LVL 5

Expert Comment

by:alicain
ID: 39807989
You just need a -Identity in there and to tweak the column names, I've added a whatif for testing...
. :
Import-CSV "E:\Projects\Users\EmailUsers.csv" | % { Set-ADUser -Identity $_.samaccountname -EmailAddress $_.Email -whaif}

Regards,
Alastair.
0
 
LVL 40

Expert Comment

by:footech
ID: 39808003
The only problem with what you have is that the property/column names in your .CSV don't match up with how you're calling them.  You use $_.User and $_.EmailAddress, while you say your .CSV has SamAccountName,Email.
Alicain's code correctly references the property names.  The -identity parameter isn't strictly necessary, as it can be referenced by position as well as by name.
0
Ready to trade in that old firewall?

Whether you need to trade-up to a shiny new Firebox or just ready to upgrade from whatever appliance you're using now, WatchGuard has the right appliance for you! Find your perfect Firebox today with appliance sizing tool!

 
LVL 2

Author Comment

by:MilesLogan
ID: 39808080
hi: becraig

I tried your code and received the error below .


Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
At E:\Projects\Users\UpdateEmail.ps1:1 char:22
+ get-aduser -identity $_.SamAccountName | Set-ADUser $_.User -EmailAddress $_.Ema ...
+                      ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
0
 
LVL 29

Expert Comment

by:becraig
ID: 39808084
get-aduser -identity $_.SamAccountName | Set-ADUser $_.User -EmailAddress $_.EmailAddress

Import-module ActiveDirectory
Import-CSV "E:\Projects\Users\EmailUsers.csv" | % {
Set-ADUser -identity $_.SamAccountName -EmailAddress $_.EmailAddress
}
                            

Open in new window

               

Should work provided the info below is correct:
The value of samaccountname is from the foreach (if correct in the csv file)
the value $_.samaccountname indicates the current value from that line in the csv file.


EmailUsers.csv file format I have
SamAccountName,Email

I think both of the guys above indicated the same as well.
0
 
LVL 2

Author Comment

by:MilesLogan
ID: 39808099
weird .. the csv file is set to what you have .

I still receive

Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
At E:\Projects\Users\UpdateEmail.ps1:1 char:22
+ get-aduser -identity $_.SamAccountName | Set-ADUser $_.User -EmailAddress $_.Ema ...
+                      ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
0
 
LVL 40

Expert Comment

by:footech
ID: 39808106
You would need to remove line 1 of becraig's code above.  It's not needed, and contains syntax errors.  Line 5 also has an error - it references "$_.EmailAddress" when it should be "$_.Email" according to your .CSV headers.
0
 
LVL 29

Accepted Solution

by:
becraig earned 400 total points
ID: 39808107
The argument is null indicates there might be null values in your csv.


Import-module ActiveDirectory
Import-CSV "E:\Projects\Users\EmailUsers.csv" | % {
while ( $_.SamAccountName -ne $null)
{Set-ADUser -identity $_.SamAccountName -EmailAddress $_.Email}
}
                           

Open in new window


                                           
This forces it to run the set-aduser only if the samaccountname is not null.
0
 
LVL 40

Assisted Solution

by:footech
footech earned 100 total points
ID: 39808110
@becraig - while that is true, the error came from running Get-ADUser and supplying it an identity in the form of $_.samaccountname when the file hadn't even been read in yet with Import-CSV and then piped to a ForEach command.
0
 
LVL 29

Expert Comment

by:becraig
ID: 39808114
Ahh :~)

I see what happened there I removed the line from the script but did not delete it so it was still at the top and he assumed it was a part of the script.

Thanks Foo
0
 
LVL 2

Author Closing Comment

by:MilesLogan
ID: 39808143
That was it !! wow thank you both !
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
Suggested Courses

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question