Solved

Powershell: Update email for multiple users in Active Directory

Posted on 2014-01-24
11
3,124 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
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help Creating PowerShell Script 4 35
Event ID 29 KDC Win 2008 R2 DC 6 16
Blocking Microsoft Edge From Running? 14 54
query all mailbox rules 5 23
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…

713 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