Isolating results by date range is eluding me...

I’m searching for users who have passwords expiring in the next seven days. everything works but the ‘where’. I’m specifying a low and high range for the dates, but I’m getting everything instead of just the seven days I want… what am I missing

#get max password age policy

#Today’s Date

#expiring in 7 days

Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False -and PasswordLastSet -gt 0} -Properties “SamAccountName”, “displayname”,“msds-userpasswordexpirytimecomputed”,“PasswordLastSet” | where {{($.PasswordLastSet).ToShortDateString() -lt $7days} -AND {($.PasswordLastSet).ToShortDateString() -gt $maxPwdDate}} | select-object -property “SamAccountName”, “displayname”, “PasswordLastSet”, @{name=“expirydate”;Expression={[datetime]::fromfiletime($_.“msds-userpasswordexpirytimecomputed”)}} | sort-object expirydate | export-csv “passwordexpiration.csv”

Here you go Rob, give this a shot. Should be pretty straight forward as to what is going on here. Let me know if anything needs explaining.

$AlertEndDate = (Get-Date).AddDays(-$maxPwdAge + 8)
$AlertBeginDate = $AlertEndDate.AddDays(-1)
Get-ADUser -Filter {
                    Enabled -eq $true -And
                    PasswordNeverExpires -eq $False -And
                    PasswordLastSet -ge $AlertBeginDate -And
                    PasswordLastSet -lt $AlertEndDate
                   } -Properties "SamAccountName", "displayname","PasswordLastSet" |
Select-Object "SamAccountName", "displayname", "PasswordLastSet", @{Label = 'PasswordExpires'; Expression = {($_.PasswordLastSet).AddDays($maxPwdAge)}}, @{Label = 'ExpiresInTimespan'; Expression = {New-TimeSpan -Start (Get-Date) -End (($_.PasswordLastSet).AddDays($maxPwdAge))}} |
Sort-Object ExpiresInTimespan


I mixed your code and my code together and got what i wanted. I had explored trying to isolate the dates in the -filter, but wasn’t getting any traction there. i will use your formatting as a model, though…

Now on to the next challenge of isolating the e-mail addresses from the users found (I added in “EmailAddress” to the Select-Object section) and looping them through a command to generate an e-mail to those users…


I’m not quite following, can you give an example of your challenge? Would probably be a good idea to start a new thread so people searching on this new challenge are not confused by the inital topic of this thread.

that was just commentary on what I was doing next. I try to only ask for help once i’ve exhausted my own attempts at problem solving…


Ah, good deal. Enjoy!