1

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
mklement0
  • 382,024
  • 64
  • 607
  • 775
Listor
  • 93
  • 9

1 Answers1

1

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
Cbsch
  • 1,164
  • 2
  • 10
  • 16
  • Perhaps add another check to get users where properties `PasswordNeverExpires` and `PasswordNotRequired` are both `$false` ? – Theo Jun 21 '20 at 14:37