Solved

Powershell: Update email for multiple users in Active Directory

Posted on 2014-01-24
11
3,003 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 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
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 39

Expert Comment

by:footech
Comment Utility
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
 
LVL 2

Author Comment

by:MilesLogan
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Author Comment

by:MilesLogan
Comment Utility
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 39

Expert Comment

by:footech
Comment Utility
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 28

Accepted Solution

by:
becraig earned 400 total points
Comment Utility
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 39

Assisted Solution

by:footech
footech earned 100 total points
Comment Utility
@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 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
That was it !! wow thank you both !
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

OfficeMate Freezes on login or does not load after login credentials are input.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now