简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM