Powershell Script for recent password changes

NOC123
NOC123 used Ask the Experts™
on
So I’m trying to write a PowerShell script that will give me a list of users who changed their password within the last N days, where N is an input I can change.  Failing that (and I have been failing), I’m working on getting a list of users sorted by password last changed date.  That I have been able to do.  However, when I use “Where-Object” to try to filter out the accounts with password never expires (role accounts, former users, etc.) that give me exactly the opposite of what I want.  
 
Get-ADUser -filter * -properties passwordlastset, passwordneverexpires | Where-Object {$_.passwordneverexpires -eq "FALSE"} | sort-object -Descending passwordlastset | select-object Name, passwordlastset, passwordneverexpires | Export-csv -path .\PWList2.csv
 
The above script gives me all the acounts where passwordneverexpires is TRUE.  If I change the comparison from “-eq” to “-ne” then I get the results I’m looking for, but the logic is backwards – that’s all the accounts where PWNeverExpires EQUALS false.  And I’ve tried using “false”, “False”, and “FALSE”, but it doesn’t seem to make any difference.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
It's a boolean property - use it as such:
Get-ADUser -Filter * -Properties PasswordLastSet, PasswordNeverExpires |
	Where-Object {-not $_.PasswordNeverExpires} |
	Sort-Object -Descending -Property PasswordLastSet |
	Select-Object -Property Name, PasswordLastSet, PasswordNeverExpires |
	Export-Csv -NoTypeInformation -Path .\PWList2.csv

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
By the way: your issue happened because in comparisons, PowerShell uses the type of the left element.
Since this is a bool, your string "FALSE" will be cast to a boolean value, which will be $true (because any non-empty string will turn out as $true).
The comparison done will then be "(bool)PasswordNeverExpires -eq $true", so it will let pass anything where PasswordNeverExpires is $true.
It would work the other way round ("FALSE" -eq $_.passwordneverexpires), because then the bool on the right side would be cast to a string and end up as "True" or "False", but that is definitely no way to treat a boolean.

Author

Commented:
Bingo!  That gets me what I want.  Now the question is, why does your negative logic work, but not mine?  It seems like the same thing, just with different syntax/terminology.  What was I doing wrong with –EQ?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
It's explained in my second comment - it's because of the way PS compares objects of different types.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial