Skip to content

Instantly share code, notes, and snippets.

@roberttoups
Last active November 3, 2020 15:41
Show Gist options
  • Save roberttoups/bd0783f605ced80e07ce68903a99ef5c to your computer and use it in GitHub Desktop.
Save roberttoups/bd0783f605ced80e07ce68903a99ef5c to your computer and use it in GitHub Desktop.
PowerShell snippet to export accounts to CSV outside the default domain password policy in Active Directory
# Export-ExceedsDomainPasswordAge.ps1
$TimeStamp = Get-Date -Format 'yyyyMMddHHmmss'
$MaxPasswordAgeInDays = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days
$MaxAgeDate = (Get-Date).AddDays(- $MaxPasswordAgeInDays)
$PropertyList = @('Enabled','PasswordLastSet','Name','GivenName','SurName','SamAccountName','UserPrincipalName','DistinguishedName','manager')
[System.Array]$ExportData = Get-ADUser -Filter { PasswordLastSet -lt $MaxAgeDate -or PasswordLastSet -notlike '*' } -Properties $PropertyList
$ExportData |
Select-Object -Property $PropertyList |
Export-Csv -Path (Join-Path -Path '.' -ChildPath "$TimeStamp-PasswordAgeOutsidePolicyAccounts.csv") -NoTypeInformation
Write-Host "$($ExportData.Count.ToString('#,##0')) accounts are outside of the Domain Password Policy Age of $MaxPasswordAgeInDays days." -ForegroundColor 'Red'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment