Skip to content

Instantly share code, notes, and snippets.

@Torxsmind
Created April 20, 2020 19:33
Show Gist options
  • Select an option

  • Save Torxsmind/4d063d69d022880d7c056ef56c83dd28 to your computer and use it in GitHub Desktop.

Select an option

Save Torxsmind/4d063d69d022880d7c056ef56c83dd28 to your computer and use it in GitHub Desktop.
Get Domain User's password dates
$now = Get-Date -UFormat "%Y-%m-%d_%H-%M-%S"
$filepath = 'C:\temp\AD_User_password_dates_' + $now + '.csv'
$ADUsers = Get-ADUser `
-Searchbase "OU=A,DC=domain,DC=com" `
-Filter {PasswordExpired -eq "False" -and PasswordNeverExpires -eq "False" -and Enabled -eq "True"} `
–Properties "DisplayName", "DistinguishedName", "msDS-UserPasswordExpiryTimeComputed", "passwordlastset", "PasswordExpired", "PasswordNeverExpires", "Enabled" `
| Where-Object {($_.DistinguishedName -notmatch "OU=1,OU=A,DC=domain,DC=com") `
-and ($_.DistinguishedName -notmatch "OU=A,OU=2,OU=A,DC=domain,DC=com") `
-and ($_.DistinguishedName -notmatch "OU=B,OU=2,OU=A,DC=domain,DC=com") `
-and ($_.DistinguishedName -notmatch "OU=2,OU=A,DC=domain,DC=com") `
-and ($_.DistinguishedName -notmatch "OU=4,OU=A,DC=domain,DC=com") `
-and ($_.DistinguishedName -notmatch "OU=5,OU=A,DC=domain,DC=com")}
$Results = foreach( $User in $ADUsers ){
$ExpireDate = (([datetime]::FromFileTime((Get-ADUser $User -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed")))
$Expirationdays = (([datetime]::FromFileTime((Get-ADUser $User -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed"))-(Get-Date)).Days
$data = @{
Name = $User.Name
LastSet = $user.PasswordLastSet
Expires = $ExpireDate
DaysRemaining = $Expirationdays
}
$obj = New-Object -Type PSObject -Prop $data
$obj | Export-Csv -Path $filepath -NoTypeInformation -append
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment