Solved

Powershell: Update email for multiple users in Active Directory

Posted on 2014-01-24
11
3,102 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains how to install and use the NTBackup utility that comes with Windows Server.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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