I am doing a powershell script that can update the Account Expires field in the AD based on sAMAccoutname
but i faced some issue on Set-ADUser
powershell command when it tried to update the Account Expires field in AD. I tested several way with various combination of command option but none of them work as expected
1st attempt
Set-ADUser -Identity xxxx00242 -accountExpires 130618739743580353
Error received
Set-ADUser : A parameter cannot be found that matches parameter name 'accountExpires'.
At line:1 char:31
+ Set-ADUser -Identity xxxx0242 -accountExpires 130618739743580353
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser
2nd attempt
Set-ADUser -identity xxxx0242 -Add @{accountExpires=" 130612691742815904"}
Error Received
Set-ADUser : The parameter is incorrect
At line:1 char:1
+ Set-ADUser -identity xxxx0242 -Add @{accountExpires=" 130612691742815 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (xxxx0242:ADUser) [Set-ADUser], ADInvalidOperationException
+ FullyQualifiedErrorId : ActiveDirectoryServer:87,Microsoft.ActiveDirectory.Management.Commands.SetADUser
3rd attempt
Set-ADUser -identity xxxx0242 -Add @{AccountExpirationDate=" 130612691742815904"}
Error Received
Set-ADUser : The specified directory service attribute or value does not exist
Parameter name: AccountExpirationDate
At line:1 char:1
+ Set-ADUser -identity xxxx0242 -Add @{AccountExpirationDate=" 13061269 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (xxxx0242:ADUser) [Set-ADUser], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Comm
ands.SetADUser
my powershell version is 5.1.14393.206 or Set-ADUser cant update Account Expires field in AD?This is the link
You need to input a valid datetime.
For instance if you want to let the account expire in 1 month.
$date = Get-Date
$dateExpiry = $date.AddMonths(1)
Set-ADUser xxxx0242 -AccountExpirationDate $dateExpiry
Please note that the returned int64 value can easily be converted to a usable datetime for instance to add 6 months to it:
$currentExpiry = (Get-ADUser xxxx0242 -Properties AccountExpires).accountExpires
[datetime]$expireDate = $currentExpiry
$newExpireDate = $expireDate.AddMonths(6)
Set-ADUser xxxx0242 -AccountExpirationDate $newExpireDate
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.