# Set-expirationdate delay

**URL:** <https://forums.powershell.org/t/set-expirationdate-delay/15370>\
**Category:** PowerShell Help\
**Created:** [December 10, 2020, 10:44pm UTC](https://forums.powershell.org/t/set-expirationdate-delay/15370 "2020-12-10T22:44:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pba1211](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pba1211/32/1421_2.png) [@pba1211](https://forums.powershell.org/u/pba1211)\
**Post date:** [December 10, 2020, 10:44pm UTC](https://forums.powershell.org/t/set-expirationdate-delay/15370/1 "2020-12-10T22:44:43Z")

</div>

Hello,

I use a script where a user is created , one of the settings is the expiration date.  
When the line for the set-expirationdate is called, powershell throws a error, that the aduser can not be found.  
I tried to workaround this, by pausing for 6 seconds to give AD the time to “think”  
This is not working, only when I do a get-aduser $username (after all is done) the usewr is present in AD.  
Then when I manually run the Set-expirationdate line, it is working fine. ( at that moment)

I am wondering why this is happening, and of course how I can fix this

```
# This will be the info needed to create the account
$ADM = "adm-fu-"
$FirstName = Read-Host "FirstName is?"
$MiddleInitial = Read-Host "What are the Middle Initals?"
$LastName = Read-Host "Lastname is?"
$Description = Read-Host "Fill in the Ticketnr, and the function"
$FJDomain = Read-host "what the e-mail domain, (like @name.com)"
$Expiration = Get-Date $((Get-Date).adddays(360)) -f 'yyyy-MM-dd'
$Emailaddress = $FirstName + '.' +$LastName + $FJDomain
$Company = Read-Host "What is the name of the Company"

$DefaultPassword= Read-Host "Create a strongpassword"

# setting the values for a adminaccount 
$Username = 'adm-fu-' + $lastname.substring(0,4) + $Firstname.substring(0,1)+'1'
    

#Create the New user Account
$NewUserParams = @{
'UserPrincipalName' = $Username
'Name' = $Username
'GivenName' = $FirstName
'Surname' = $LastName
'DisplayName' = $FirstName + $MiddleInitial + $LastName
'Description' = $Description
'EmailAddress'= $Emailaddress
'SamAccountName' = $Username
'AccountPassword' = (ConvertTo-SecureString $DefaultPassword -AsPlainText -Force)
'CannotChangePassword' = $false
'Enabled' = $True
'Initials' = $MiddleInitial
'Path' = "$OU,$Dn"
'Company' = $company
#'AccountExpirationDate' = Set-ADAccountExpiration -DateTime $Expiration

#Because of COVID and RDP, do not set " change at next logon" 
'ChangePasswordAtLogon' = $false
}

#Create the new user account
New-AdUser @NewUserParams

Sleep -Seconds 6

#set-expirationdate
Set-ADAccountExpiration $username -DateTime $Expiration
```

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [December 10, 2020, 10:52pm UTC](https://forums.powershell.org/t/set-expirationdate-delay/15370/2 "2020-12-10T22:52:10Z")

</div>

Specify the same domain controller with the -Server parameter for both cmdlets. You’re probably hitting a different DC when you run Set-ADAccountExpiration and the new account hasn’t replicated.

---

<div class="post-metadata">

**Author:** ![pba1211](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pba1211/32/1421_2.png) [@pba1211](https://forums.powershell.org/u/pba1211)\
**Post date:** [December 13, 2020, 8:37pm UTC](https://forums.powershell.org/t/set-expirationdate-delay/15370/3 "2020-12-13T20:37:20Z")

</div>

Hello Matt,

Thank you for pointing the right direction.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/set-expirationdate-delay/15370/4 "2024-05-16T20:28:10Z")

</div>


