0
votes

I am trying to figure out how to script something that will search AD and set the expiration date of the users based on their creation date, example user created 01/01/2018 they will expire 01/01/2019.

Get-ADUser -Filter * -SearchBase $OuDomain | Set-ADAccountExpiration -DateTime # this is the part where I cannot figure out the select-object Created + 365

Update- I achieved this by exporting users by their username and created date to csv, then manipulated that with cel+365 to create a time stamp of 1 year. Imported that csv via PS then applied the below script to apply the expiration date to the users listed.

foreach ($Users in $Users) {Set-ADUser $Users.Username -AccountExpirationDate $Users.Expires}

2

2 Answers

0
votes

You will need to set a variable for the AD attribute you look to reference and then call on it in an "if statement"

$getexpiration = Get-ADUser -Filter * -SearchBase $OuDomain -Properties AccountExpires | select-object accountexpires

This should target the field in AD you need but from here you need to write an if statement to update this in AD.

0
votes

thanks. I did manage to get the below so far this afternoon-

PS C:\Users\Administrator> $Exp = Get-ADUser -Filter * -SearchBase $OU -Properties * | Select created PS C:\Users\Administrator> $exp | fl

created : 31/08/2018 10:01:58

created : 04/09/2018 10:22:12

PS C:\Users\Administrator> Get-ADUser -Filter * -SearchBase $OU | Set-ADAccountExpiration -DateTime $Exp Set-ADAccountExpiration : Cannot convert 'System.Object[]' to the type 'System.Nullable`1[System.DateTime]' required by parameter 'DateTime'. Specified method is not supported. At line:1 char:74 + Get-ADUser -Filter * -SearchBase $OU | Set-ADAccountExpiration -DateTime $Exp + ~~~~ + CategoryInfo : InvalidArgument: (:) [Set-ADAccountExpiration], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADAccountExpirati on