Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, September 16, 2014 9:49 PM
Hello,
I have a script below that does not work below and I am simply updating one field in the profile in AD. I am importing a CSV file, which is below:
SamAccountName, HireDate
bjones,2/14/1999
mloo, 5/12/2001
In the CSV, what would be the correct format? HireDate is a custom attribute.
The below PowerShell script error is below:
Set-ADUser : A parameter cannot be found that matches parameter name
'HireDate'.
At line:4 char:14
- Set-ADUser -HireDate $($user.HireDate)
- ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBind
ingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory
.Management.Commands.SetADUser
Here is my script:
Import-Module ActiveDirectory
$users = Import-Csv -Path d:\scripts\hiredatetest.csv
foreach ($user in $users)
{
Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "cn=Users,DC=domain,DC=com" |
Set-ADUser -HireDate $($user.HireDate)
}
Any advice on how to get this working would be great help.
Paul
All replies (3)
Wednesday, September 17, 2014 9:09 AM ✅Answered | 2 votes
Hi Paul,
To update extension attribute of users, Please use the parameter "-Replace":
Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "cn=Users,DC=domain,DC=com" |Set-ADUser -Replace @{HireDate=$($user.HireDate)}
Using the cmdlet "Set-ADuser", please refer to these articles:
http://technet.microsoft.com/en-us/library/ee617215.aspx
http://richardspowershellblog.wordpress.com/2012/03/22/updating-ad-users-in-bulk/
If there is anything else regarding this issue, please feel free to post back.
Best Regards,
Anna Wang
TechNet Community Support
Wednesday, September 24, 2014 9:46 AM ✅Answered
Hi Paul,
This error would occur because some users haven't listed the hiredate value in the csv file, please try this script:
Import-Module ActiveDirectory
$users = import-csv -path d:\scripts\hiredatetest.csv
foreach($user in $users)
{
$length=$user.hiredate.Length
if($length -ge 1){ #if there has hiredate value in the csv file then change the value
$fielduser = $user.SamAccountName
$fielddate = $user.hiredate -as [datetime]
echo "$fielduser,$fielddate"
Get-ADUser -Filter "SamAccountName -eq '$($user.SamAccountName)'" -Properties * -SearchBase "DC=domain,DC=com" |
Set-ADUser -Replace @{hiredate=$($user.hiredate)}
}
}
If there is anything else regarding this script, please feel free to post back.
Best Regards,
Anna Wang
Thursday, September 18, 2014 10:21 PM
Hello,
Thanks for help, but not quite working and still need some help. I get errors and below is my script.
Import-Module ActiveDirectory
$users = import-csv -path d:\scripts\hiredatetest.csv
foreach($user in $users)
{
$fielduser = $user.SamAccountName
$fielddate = $user.hiredate -as [datetime]
echo "$fielduser,$fielddate"
Get-ADUser -Filter "SamAccountName -eq '$($user.SamAccountName)'" -Properties * -SearchBase "DC=domain,DC=com" |
Set-ADUser -Replace @{hiredate=$($user.hiredate)}
}
Errors are:
Set-ADUser : The parameter is incorrect
At line:7 char:1
- Set-ADUser -Replace @{hiredate=$($user.hiredate)}
-
+ CategoryInfo : InvalidOperation: (CN=jones\ Paul...,DC=domain,DC
=com:ADUser) [Set-ADUser], ADInvalidOperationException
+ FullyQualifiedErrorId : The parameter is incorrect,Microsoft.ActiveDirec
tory.Management.Commands.SetADUser
,
Set-ADUser : Cannot bind parameter 'Replace' to the target. Exception setting
"Replace": "Object reference not set to an instance of an object."
At line:7 char:21
- Set-ADUser -Replace @{hiredate=$($user.hiredate)}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (:) [Set-ADUser], ParameterBindingEx
ception
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory
.Management.Commands.SetADUser
Paul