Skip to content

Instantly share code, notes, and snippets.

@PSingletary
Created June 4, 2020 17:27
Show Gist options
  • Save PSingletary/19a8fe89a5285a8d49443542a333fcdb to your computer and use it in GitHub Desktop.
Save PSingletary/19a8fe89a5285a8d49443542a333fcdb to your computer and use it in GitHub Desktop.
Get the password expiration date of a samAccountName (AD User object)
<#
.SYNOPSIS
Gets samaccountname password expiration date
.DESCRIPTION
Quickly determine the password expiration date of an Ad User account:
Good - got some time before password expires
Changes soon - password needs to be changed in 11 days
Expired
.EXAMPLE
PS C:\> Get-Password_Expiration.ps1
follow the prompts
.INPUTS
samAccountName
.OUTPUTS
when password exires
#>
Import-Module ActiveDirectory
$wshell = New-Object -ComObject Wscript.Shell # Popup Window. Source: https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/x83z1d9f(v=vs.84)
$popuptimer = 3
$SAM = Read-Host -Prompt "Enter the SamAccountName"
$ADUser = Get-ADUser -Identity $SAM -Properties DisplayName, msDS-UserPasswordExpiryTimeComputed
$DisplayName = $ADUser.DisplayName
$ExpiryDate = [datetime]::FromFileTime($ADUser."msDS-UserPasswordExpiryTimeComputed")
$Now = Get-Date
$in11 = $Now + 11
#Indicate Expiry Status
if ($ExpiryDate -gt $in11) {
$State = "Good"
$Color = "Green"
}
else {
if ($ExpiryDate -gt $Now) {
$State = "going to require a CHANGE SOON"
$Color = "Yellow"
}
else {
if ($ExpiryDate -le $Now) {
$State = "EXPIRED"
$Color = "Red"
}
}
}
#it's GO TIME!
$Message = "$SAM password is $State. Date of Expiration - $ExpiryDate"
$wshell.Popup($Message, $popuptimer, $State, 0x40)
Clear-Host
Write-Host $Message -ForegroundColor Black -BackgroundColor $Color
Set-Clipboard $Message
Write-Host "copied to clipboard" -ForegroundColor Black -BackgroundColor $Color
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment