'Powershell Creating Password Expiration Report

I am trying to create a password expiration report that runs once a week and I want to grab 7 days of it at a time.

The current script that I have been creating doesn't seem to pull that. I get everything that is today and back, but never the next day, let alone 7 days. I believe that I am not pulling the correct dates but I do not know how to do so. I have tried asking others and googling my issue but have not found a way.

I understand that I may have left a "-2" in here but that was to test it. Everything works up till the "?" and if I don't have the "-or" then I get a Null so something in the where-object is not working

Email sends just fine as well.

[cmdletbinding()]
Param (
    [parameter(DontShow = $true)]
    $SmartHost = 'smtpserver.company.com',

    [parameter(DontShow = $true)]
    $SendTo = 'someone',

    [parameter(DontShow = $true)]
    $SendFrom = "me",

    [parameter(DontShow = $true)]
    $MailSubject = "Daily Password Expiry Report"
)

process {
    Write-Verbose -Message "Collecting data..."
    $mailBody = ((Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False -and PasswordLastSet -gt 0}  -Properties "DisplayName", "msDS-UserPasswordExpiryTimeComputed" | Sort msDS-UserPasswordExpiryTimeComputed | 
    Select-Object -Property "Displayname",@{Name="ExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}})|
     ? {($_.expiryDate -eq (Get-Date).AddDays(-2) -or $_.expiryDate -lt (Get-Date)) }| Out-String)
}

end {
    Write-Verbose -Message "Sending Mail..."
    Send-MailMessage -Subject $mailSubject -From $sendFrom -To $sendTo -SmtpServer $smartHost -body $mailBody

}


Solution 1:[1]

How about leveraging this one and tweaking as needed.

Password Expiration Report and User Notification Powershell Script

Original Portions of this script attributed to Martin Pugh (www.thesurlyadmin.com). I just cleaned the original up and added some fancier email notification and added some additional functionality.You can configure this script as a scheduled task to both get a report of users who

https://gallery.technet.microsoft.com/scriptcenter/Password-Expiration-Report-478037a5

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 postanote