Solved

Powershell: Update email for multiple users in Active Directory

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

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
 
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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 39

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 39

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
A brief introduction to what I consider to be the best editor for PowerShell.
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
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…

910 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

21 Experts available now in Live!

Get 1:1 Help Now