powershelldatefilter

Powershell password expiration date filtering


Im currently trying to pull a CSV that returns users whose AD password is expiring within a date range. I currently have the code below, which pulls all users and expiration dates and emails. Was wondering how to filter this statement by date so I could know all users 5 days from expiration?

get-aduser -filter * -properties "DisplayName", "msDS-UserPasswordExpiryTimeComputed" , EmailAddress, DisplayName | Select-Object -Property "Displayname", EmailAddress,@{Name="Expiration Date";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}} | Export-Csv -Path c:\support\PasswordExpiration.csv -Encoding ascii -NoTypeInformation

Solution

  • Add in a Where-Object before the Export-Csv. This will return all the users that have an expiration date less than 5 days in the future

    Get-ADUser -Filter * -Properties "DisplayName", "msDS-UserPasswordExpiryTimeComputed" , EmailAddress, DisplayName | 
        Select-Object -Property "Displayname", EmailAddress,@{Name="Expiration Date";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}} | 
        Where-Object { $_.'Expiration Date' -lt (Get-Date).AddDays(5) } |
        Export-Csv -Path c:\support\PasswordExpiration.csv -Encoding ascii -NoTypeInformation