Skip to content

Instantly share code, notes, and snippets.

@shinjijai
Created December 16, 2019 19:07
Show Gist options
  • Save shinjijai/506e5966568c9c676434e77b5d44f881 to your computer and use it in GitHub Desktop.
Save shinjijai/506e5966568c9c676434e77b5d44f881 to your computer and use it in GitHub Desktop.
Check ActiveDirectory user's password status
#Requires -Version 4.0
#Requires -Module ActiveDirectory
<#
.SYNOPSIS
Returns information on the user(s) account status.
.DESCRIPTION
Connects to the Active Directory that you are close to and pulls information from it.
Which means that information about the user that are not sync between ActiveDirectory
will not be accurate. But information like Days since password was set, will be valid, no matter what.
#>
Import-Module ActiveDirectory -ErrorAction SilentlyContinue
do {
# Ask for the name of the user to search for
Clear-Host
$User = Read-Host "Please enter a name "
$User = "*$User*"
# Searches the AD to find any object that matches the criteria and then outputs
# the information with the Name, password status, and if the account is disable
$SelectParams = @(
"Name",
@{Name = "Password status"; Expression = {$_.LockedOut}},
@{Name = "Enabled status"; Expression = {$_.Enabled}},
@{Name = "Last logon"; Expression = {[DateTime]::FromFileTime($_.LastLogonTimeStamp)}},
@{Name = "Last password set"; Expression = {(Get-Date($_.PasswordLastSet)).ToLocalTime()}},
@{Name = "Last bad password"; Expression = {[DateTime]::FromFileTime($_.BadPasswordTime)}},
@{Name = "Days since password was set"; Expression = {
New-TimeSpan -Start (Get-Date($_.PasswordLastSet)) -End (Get-Date) | Select-Object -ExpandProperty Days
}}
)
Get-ADUser -Filter {(SAMAccountName -like $User) -or (DisplayName -like $User)} -Properties * | Select-Object $SelectParams | Format-List
$SelSearch = Read-Host "Do another search for user(s) password status? (y/n)"
try {
$Ans = $SelSearch.Substring(0,1)
} catch {
Write-Host "Exiting..."
}
} while ($Ans -eq "y")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment