How do I add 90 days to an AD account's existing expiration RRS feed

  • Question

  • I realize that it's a simple matter to use the Set-ADAccountExpiration and TimeSpan switch
    $ID = SomeTestAccount
    Set-ADAccountExpiration $ID -TimeSpan 90.0:0

    However, what I've been asked to implement is something that would read the user account's current expiration and add 90 days to it.  Once I have the methodology to run this against one account, I can use Import-CSV and ForEach to read through a list of accounts that will be generated through another process.  I'm hoping for something like:
    Get-ADUser -Identity $ID -Properties AccountExpirationDate
    $NewExpiration = $ID.AccountExpirationDate + 90 Days
    Set-ADAccountExpiration -Identity $ID -DateTime $NewExpiration

    • Edited by Dave Megyesi Thursday, May 14, 2015 6:49 PM Typo
    Thursday, May 14, 2015 6:48 PM


All replies

  • Hi,

    Here's an example:

    $user = Get-ADUser tester1 -Properties AccountExpirationDate
    $newExpirationDate = ($user.AccountExpirationDate).AddDays(90)

    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Thursday, May 14, 2015 7:08 PM
  • That worked wonderfully.  I'm going to add some to this, but here's the start of something usable.
    $File = Import-CSV .\ExtendExpirationDate.csv
        ForEach ($Line in $File)
        $ID = $Line.account
        $accountexpire = Get-ADUser $ID -Properties AccountExpirationDate
        $NewExpirationDate = ($accountexpire.AccountExpirationDate).AddDays(90)
        Set-ADAccountExpiration -Identity $ID -DateTime $NewExpirationDate

    I'm working out how I want to report out the results after it runs.   I've noticed it takes a couple of seconds before a Get-ADUser will report on the new date, whereas if you simply specified the date the cmdlet reports immediate results.  I can easily work around that for reporting, but if others find this post useful, they might want to know that as well.
    Thursday, May 14, 2015 8:20 PM