First, thank you for bringing msDS-UserPasswordExpiryTimeComputed to my attention. I don’t recall coming across this. I am not sure how desirable it is to use it based on my Googling. It’s a calculated value when used, so that does add some overhead to the script that may or may not be a good thing. Just food for thought.
You are close to having a working script.
Some thoughts that should help you get it all working
# Original
# $PasswordExpireLimitDate = (Get-Date).AddDays(30).ToLongDateString()
# As Olaf suggested, this works
$PasswordExpireLimitDate = (Get-Date).AddDays(30)
There are some errors in your Get-Aduser. See if you can find them.
Tips. Review the filter rules and how to handle Booleans in a filter. Review the PS quoting rules. And about pipelines
$ADUserList = Get-ADUser -Filter “Enabled -eq ‘$true’ -and PasswordNeverExpires -eq ‘$False’” -Properties ‘DisplayName’, ‘msDS-UserPasswordExpiryTimeComputed’, ‘emailaddress’ | ft |
If you want to convert each user’s msDS-UserPasswordExpiryTimeComputed value, then compare it to find how many days until expire? This is in the wrong place.
$PasswordExpireDate = [datetime]::FromFileTime($($ADUser.‘msDS-UserPasswordExpiryTimeComputed’)).Date
When you get it to the right place, you will also have to change this part
$ADUser.‘msDS-UserPasswordExpiryTimeComputed’
You should rethink this entire area. It is what Krzydoug is referring to his post.
{
Get-ADUser | Select-Object -Property “Name”, “EmailAddress”
}
Since there is no filter, the script will stop till you provide one. If you use *, that pulls in everyone. If you don’t want everyone, you have to provide something more specific. Since you already have all the AD accounts you need at time of script being run, this part makes no sense.
If you are trying to see who matches your conditions and has a password expiring in 30 days, you do not need to use Get-ADuser again. You already have that info. First in $ADUserList. This is your entire collection of users that match your original get-aduser search.
Then later as individual users in your foreach loop you pull one user at a time from $ADuserlist and place them into $Users.
When you get into the IF section, you want some kind of output for the results. You have several options such as write-host, create custom object to collect the results and place in a csv file, create html report etc.
A simple example to at least verify your basic script would be something like this. Then once you confirm everything is working, you can then get fancy with how you capture the results.
if ($PasswordExpireDate -lt $PasswordExpireLimitDate) {
$ExpireDaySpan = New-TimeSpan -Start (Get-Date).Date -End $PasswordExpireDate
Write-host "$($user.name) password is expiring in $ExpireDaySpan "
}
}
about Quoting Rules - PowerShell | Microsoft Learn
Get-ADUser (ActiveDirectory) | Microsoft Learn
about Pipelines - PowerShell | Microsoft Learn