Skip to content

Instantly share code, notes, and snippets.

@JoeDibley
Last active August 9, 2018 09:56
Show Gist options
  • Save JoeDibley/a656570a652e2c94434fd8db5cca909e to your computer and use it in GitHub Desktop.
Save JoeDibley/a656570a652e2c94434fd8db5cca909e to your computer and use it in GitHub Desktop.
Function to retrieve Active Directory Password Expiration Time
<#
.SYNOPSIS
Get-ADPasswordExpiration is used to easily retrieve active directory password expiration
time using the calculated attribute msDS-UserPasswordExpiryTimeComputed
Requires ActiveDirectory PowerShell module
.PARAMETER Username
Username, Samaccountname or DistinguishedName of the accounts required.
Accepts array values
.EXAMPLE
PS C:\> Get-ADPasswordExpiration -Username jdibley
This example shows how to call the Get-ADPasswordExpiration function with a single value
.EXAMPLE
PS C:\> Get-ADPasswordExpiration -Username (Get-Content C:\Usernames.txt)
This example shows how to call the Get-ADPasswordExpiration function with a single value
.INPUTS
System.String[]
.OUTPUTS
Microsoft.ActiveDirectory.Management.ADUser
#>
Import-Module ActiveDirectory
function Get-ADPasswordExpiration {
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$true)]
[System.String[]]
[Alias('Samaccountname','DistinguishedName')]
$Username
)
#Requires -Module ActiveDirectory
begin {
}
process
{
foreach ($User in $Username)
{
try
{
Get-ADUser $User -Properties "Mail", "DisplayName", "msDS-UserPasswordExpiryTimeComputed" -ErrorAction Stop |
Select-Object -Property "Displayname", "Samaccountname", "mail" @{
Name = "ExpiryDate"; Expression = {
[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")
}
}
}
catch
{
Write-Error -Message "Failed to find: $User"
}
}
}
end
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment