I can get a valid "msDS-UserPasswordExpiryTimeComputed" property for a single user:
(([DateTime]::FromFileTime((Get-ADUser $UserName -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed")))
But I'm having trouble targeting a group of specific users. My larger script is intended to pull various AD user attributes from accounts in particular OUs and export to csv. All of the properties are populated as expected, except for the "PasswordExpiry" object.
The below sample returns a bogus "PasswordExpiry" date of "12/31/1600" for every user. "C:\UserList.txt" contains sAMAccountNames one per line.
$UserList=Get-Content "C:\UserList.txt"
ForEach ($UserName in $UserList) {Get-ADUser "$UserName" -Properties * |
Select-Object sAMAccountName,whenCreated, `
@{Name="lastLogon";Expression={[DateTime]::FromFileTime($_.lastLogon)}}, `
@{Name="pwdLastSet";Expression={[DateTime]::FromFileTime($_.pwdLastSet)}}, `
@{Name="PasswordExpiry";Expression={[DateTime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}, `
cannotChangePassword,passwordNeverExpires, `
@{Name="GroupMember";Expression={($_ | Select -ExpandProperty MemberOf) | Where {$_ -Like "*Desired.Group*"}}} |
Export-Csv -Path "C:\UserInfo.csv" -Append -NoTypeInformation}
This works if I wanted to query all AD users:
Get-ADUser -Filter * –Properties sAMAccountName,"msDS-UserPasswordExpiryTimeComputed" |
Select-Object -Property sAMAccountName,@{Name="PasswordExpiry";Expression={[DateTime]::FromFileTime($_.“msDS-UserPasswordExpiryTimeComputed”)}}
But if I add the [-SearchBase "OU=Users,DC=Domain,DC=local"] parameter to Get-ADUser, I get null output for "PasswordExpiry". I guess I could try parsing the entire output with some post processing. Seems like touching more than I should have to though.
I know that I can calculate the expiration based off the "pwdLastSet" attribute. But I want to pull the actual value because age policy might be unknown. Any help is appreciated.