Last active
August 9, 2018 09:56
-
-
Save JoeDibley/a656570a652e2c94434fd8db5cca909e to your computer and use it in GitHub Desktop.
Function to retrieve Active Directory Password Expiration Time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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