MilesLogan
asked on
Powershell: Update email for multiple users in Active Directory
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\EmailUs ers.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\UpdateEm ail.ps1:3 char:12
+ Set-ADUser $_.User -EmailAddress $_.EmailAddress
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Act iveDirecto ry.Managem ent.Comman ds.SetADUs er
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\EmailUs
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\UpdateEm
+ Set-ADUser $_.User -EmailAddress $_.EmailAddress
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
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\EmailUs ers.csv" | % { Set-ADUser -Identity $_.samaccountname -EmailAddress $_.Email -whaif}
Regards,
Alastair.
. :
Import-CSV "E:\Projects\Users\EmailUs
Regards,
Alastair.
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.
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.
ASKER
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\UpdateEm ail.ps1:1 char:22
+ get-aduser -identity $_.SamAccountName | Set-ADUser $_.User -EmailAddress $_.Ema ...
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Act iveDirecto ry.Managem ent.Comman ds.GetADUs er
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\UpdateEm
+ get-aduser -identity $_.SamAccountName | Set-ADUser $_.User -EmailAddress $_.Ema ...
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
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
}
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.
ASKER
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\UpdateEm ail.ps1:1 char:22
+ get-aduser -identity $_.SamAccountName | Set-ADUser $_.User -EmailAddress $_.Ema ...
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Act iveDirecto ry.Managem ent.Comman ds.GetADUs er
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\UpdateEm
+ get-aduser -identity $_.SamAccountName | Set-ADUser $_.User -EmailAddress $_.Ema ...
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
That was it !! wow thank you both !
Open in new window