<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Get Password Expiration Date Using Powershell Archives - the Sysadmin Channel</title>
	<atom:link href="https://thesysadminchannel.com/tag/get-password-expiration-date-using-powershell/feed/" rel="self" type="application/rss+xml" />
	<link>https://thesysadminchannel.com/tag/get-password-expiration-date-using-powershell/</link>
	<description>Documenting My Life as a System Administrator</description>
	<lastBuildDate>Mon, 04 Apr 2022 02:12:03 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>
<site xmlns="com-wordpress:feed-additions:1">144174110</site>	<item>
		<title>Get Password Expiration Date Using Powershell</title>
		<link>https://thesysadminchannel.com/get-password-expiration-date-using-powershell-active-directory/</link>
					<comments>https://thesysadminchannel.com/get-password-expiration-date-using-powershell-active-directory/#comments</comments>
		
		<dc:creator><![CDATA[Paul Contreras]]></dc:creator>
		<pubDate>Mon, 02 Mar 2020 07:42:00 +0000</pubDate>
				<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[active directory expired password query]]></category>
		<category><![CDATA[get password expiration date powershell]]></category>
		<category><![CDATA[Get Password Expiration Date Using Powershell]]></category>
		<category><![CDATA[how to check when password expires in active directory powershell]]></category>
		<category><![CDATA[msds-userpasswordexpirytimecomputed]]></category>
		<category><![CDATA[powershell - get account expiration date]]></category>
		<category><![CDATA[powershell password expiration report]]></category>
		<category><![CDATA[powershell password expires in 7 days]]></category>
		<category><![CDATA[powershell script to get password expiration date]]></category>
		<guid isPermaLink="false">https://thesysadminchannel.com/?p=2075</guid>

					<description><![CDATA[<p>A while back I posted a Powershell script to check password expiration&#8217;s in your domain. It worked and it got the job done but as I got better with Powershell I decided to take another look at it and refine&#8230; <a href="https://thesysadminchannel.com/get-password-expiration-date-using-powershell-active-directory/" class="more-link">Continue Reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a href="https://thesysadminchannel.com/get-password-expiration-date-using-powershell-active-directory/">Get Password Expiration Date Using Powershell</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>A while back I posted a Powershell script to <a href="https://thesysadminchannel.com/powershell-script-check-password-expirations-in-active-directory/" rel="noopener noreferrer" target="_blank">check password expiration&#8217;s in your domain</a>.  It worked and it got the job done but as I got better with Powershell I decided to take another look at it and refine it. This time around I&#8217;ve turned it into a function with the ability to set specific days you want to filter from as well as explicitly adding the send email option.  I&#8217;ve also added a parameter so you can check specific users to see when the next time they&#8217;ll need to change their password.  Feel free to comment on the new script to <strong>Get Password Expiration Date Using Powershell</strong></p>
<h2>Get Password Expiration Date Using Powershell</h2>
<p>The only requirement is that you&#8217;ll need the Active Directory Powershell module to be able to query that the information stored in AD. Also, if you plan on using the send email parameter you&#8217;ll need to modify lines 88-92 so you can send it out of your own smtp server.</p>
<pre class="brush: powershell; title: ; notranslate">

Function Get-PasswordExpirationDate {
#requires -Module ActiveDirectory

&lt;#
.SYNOPSIS
    Checks to see if the account is X days within password expiration.
    For updated help and examples refer to -Online version.

.NOTES
    Name: Get-PasswordExpirationDate
    Version: 2.0
    Author: theSysadminChannel
    DateCreated: 2019-Dec-15

.LINK
    https://thesysadminchannel.com/get-password-expiration-date-using-powershell-active-directory -



.PARAMETER DaysWithinExpiration
    Set the number of days you want to check until the password is expired.  Valid options are 1 - 365.

.PARAMETER SendEmail
    Send an email to each user that has the EmailAddress populated to notify them that their password is nearning expiration.

.PARAMETER SamAccountName
    Specify the user accounts you want to check.  This option does not support the SendEmail or DaysWithinExpiration parameters.

.EXAMPLE
    Get-PasswordExpirationDate 15

.EXAMPLE
    Get-PasswordExpirationDate -DaysWithinExpiration 10 -SendEmail

.EXAMPLE
    Get-PasswordExpirationDate -SamAccountName Username1, username2

#&gt;

    [CmdletBinding(DefaultParameterSetName=&quot;AllAccounts&quot;)]
    param(
        [Parameter(
            Position = 0,
            Mandatory = $false,
            ParameterSetName = &quot;AllAccounts&quot;
        )]
        [ValidateRange(1,365)]
        [int]       $DaysWithinExpiration = 10,


        [Parameter(
            Mandatory = $false,
            ParameterSetName = &quot;AllAccounts&quot;
        )]
        [switch]    $SendEmail,


        [Parameter(
            Mandatory = $false,
            ParameterSetName = &quot;SpecificAccounts&quot;,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true
            )]
        [string[]]  $SamAccountName
    )

    BEGIN {}

    PROCESS {
        #Calculating the expired date from the domain's default password policy. -- Do Not Modify --
        $MaxPwdAge   = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days
        $expiredDate = (Get-Date).addDays(-$MaxPwdAge)

        #Calculating the number of days until you would like to begin notifying the users. -- Do Not Modify --
        $emailDate = (Get-Date).addDays(-($MaxPwdAge - $DaysWithinExpiration))

        #Since specific accounts were specified we'll output their password expiration dates regardless if they are within the expiration date
        #Use msDS-UserPasswordExpiryTimeComputed to calculate expiration date in case a fine grain password policy is used
        if ($PSBoundParameters.ContainsKey(&quot;SamAccountName&quot;)) {
            foreach ($User in $SamAccountName) {
                try {
                    $ADObject = Get-ADUser $User -Properties PasswordNeverExpires, PasswordLastSet, EmailAddress, msDS-UserPasswordExpiryTimeComputed
                    if ($ADObject.PasswordNeverExpires -eq $true) {
                        $DaysUntilExpired = &quot;NeverExpire&quot;
                      } else {
                        $ExpirationDate = Get-Date ([datetime]::FromFileTime($ADObject.'msDS-UserPasswordExpiryTimeComputed'))
                        $DaysUntilExpired = $ExpirationDate - (Get-Date) | select -ExpandProperty Days
                    }
                    [PSCustomObject]@{
                        SamAccountName   = $ADObject.samaccountname.toLower()
                        PasswordLastSet  = $ADObject.PasswordLastSet
                        ExpirationDate   = $ExpirationDate
                        DaysUntilExpired = $DaysUntilExpired
                        EmailAddress     = $ADObject.EmailAddress
                    } 
               } catch {
                    Write-Error $_.Exception.Message
                }
            }
        } else {
            $ExpiredAccounts = Get-ADUser -Filter {(PasswordLastSet -lt $EmailDate) -and (PasswordLastSet -gt $ExpiredDate) -and (PasswordNeverExpires -eq $false) -and (Enabled -eq $true)} -Properties PasswordNeverExpires, PasswordLastSet, EmailAddress
            foreach ($ADObject in $ExpiredAccounts) {
                try {
                    $DaysUntilExpired = $ADObject.PasswordLastSet - $ExpiredDate | select -ExpandProperty Days
                    if ($PSBoundParameters.ContainsKey(&quot;SendEmail&quot;) -and $null -ne $ADObject.EmailAddress) {
                        #Setting up email parameters to send a notification email to the user
                        $From       = &quot;example@thesysadminchannel.com&quot;
                        $Subject    = &quot;Your Password Will Expire in &quot; + $DaysUntilExpired + &quot; days&quot;
                        $Body       = &quot;Hello,`n`nThis email is to notify you that your password will expire in &quot; + $DaysUntilExpired + &quot; days.`n`nPlease consider changing it to avoid any service interruptions.`n`nThank you,`nThe I.T. Department.&quot;
                        $smtpServer = &quot;mail.thesysadminchannel.com&quot;
                        #$CC        =  &quot;cc1@thesysadminchannel.com&quot;, &quot;cc2@thesysadminchannel.com&quot;

                        Send-MailMessage -To $($ADObject.EmailAddress) -From $From -Subject $Subject -BodyAsHtml $Body -SmtpServer $SmtpServer #-Priority High -Cc $CC
                    }
                    [PSCustomObject]@{
                        SamAccountName   = $ADObject.samaccountname.toLower()
                        PasswordLastSet  = $ADObject.PasswordLastSet
                        DaysUntilExpired = $DaysUntilExpired
                        EmailAddress     = $ADObject.EmailAddress
                    }
                } catch {
                    Write-Error $_.Exception.Message
                }
            }
        }
    }

    END {}

}

</pre>
<p>&nbsp;</p>
<p>Once you call the function here&#8217;s an example of what the output would look like.<br />
<a href="https://thesysadminchannel.com/wp-content/uploads/2020/08/Get-PasswordExpirationDate-DaysWithinExpiration-10-SendEmail.png" target="_blank" rel="noopener noreferrer"><img fetchpriority="high" decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2020/08/Get-PasswordExpirationDate-DaysWithinExpiration-10-SendEmail.png" alt="Get-PasswordExpirationDate-DaysWithinExpiration-10-SendEmail" width="1219" height="692" class="aligncenter size-full wp-image-2079" srcset="https://thesysadminchannel.com/wp-content/uploads/2020/08/Get-PasswordExpirationDate-DaysWithinExpiration-10-SendEmail.png?v=1597602731 1219w, https://thesysadminchannel.com/wp-content/uploads/2020/08/Get-PasswordExpirationDate-DaysWithinExpiration-10-SendEmail-1024x581.png?v=1597602731 1024w, https://thesysadminchannel.com/wp-content/uploads/2020/08/Get-PasswordExpirationDate-DaysWithinExpiration-10-SendEmail-768x436.png?v=1597602731 768w" sizes="(max-width: 1219px) 100vw, 1219px" /></a></p>
<p><div id="attachment_2081" style="width: 949px" class="wp-caption aligncenter"><a href="https://thesysadminchannel.com/wp-content/uploads/2020/08/PasswordNotificationEmail.png" target="_blank" rel="noopener noreferrer"><img decoding="async" aria-describedby="caption-attachment-2081" src="https://thesysadminchannel.com/wp-content/uploads/2020/08/PasswordNotificationEmail.png" alt="PasswordNotificationEmail" width="939" height="462" class="size-full wp-image-2081" srcset="https://thesysadminchannel.com/wp-content/uploads/2020/08/PasswordNotificationEmail.png?v=1597603934 939w, https://thesysadminchannel.com/wp-content/uploads/2020/08/PasswordNotificationEmail-768x378.png?v=1597603934 768w" sizes="(max-width: 939px) 100vw, 939px" /></a><p id="caption-attachment-2081" class="wp-caption-text">You can customize the email to send as high priority or add CC if you like when you specify the -SendEmail parameter</p></div><br />
&nbsp;</p>
<p>Here&#8217;s an example if you wanted to specify users.  If the password is set to not expire it will display NeverExpire.  It will also error out if a user is not found in Active Directory.<br />
<a href="https://thesysadminchannel.com/wp-content/uploads/2020/08/Get-PasswordExpirationDate-SamAccountName.png" target="_blank" rel="noopener noreferrer"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2020/08/Get-PasswordExpirationDate-SamAccountName.png" alt="Get-PasswordExpirationDate-SamAccountName" width="1219" height="380" class="aligncenter size-full wp-image-2083" srcset="https://thesysadminchannel.com/wp-content/uploads/2020/08/Get-PasswordExpirationDate-SamAccountName.png?v=1597605461 1219w, https://thesysadminchannel.com/wp-content/uploads/2020/08/Get-PasswordExpirationDate-SamAccountName-1024x319.png?v=1597605461 1024w, https://thesysadminchannel.com/wp-content/uploads/2020/08/Get-PasswordExpirationDate-SamAccountName-768x239.png?v=1597605461 768w" sizes="(max-width: 1219px) 100vw, 1219px" /></a></p>
<p>So now that we&#8217;ve got a working Powershell script to query Active Directory for expired passwords,  we can also use this as a Powershell password expiration report which is really nice.</p>
<p>All in all I wanted to say thanks a lot for taking the time to visit and hopefully you can make use of the <strong>get password expiration date Powershell</strong> script in your environment.  If you like these kinds of posts, feel free to check out our <a href="https://thesysadminchannel.com/powershell/" rel="noopener noreferrer" target="_blank">gallery full of useful real-world scripts</a>.  Don&#8217;t forget to check out our <a href="https://www.youtube.com/c/TheSysadminChannel" rel="noopener noreferrer" target="_blank">Youtube Page for sysadmin video content</a>.</p>
<p>The post <a href="https://thesysadminchannel.com/get-password-expiration-date-using-powershell-active-directory/">Get Password Expiration Date Using Powershell</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://thesysadminchannel.com/get-password-expiration-date-using-powershell-active-directory/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2075</post-id>	</item>
	</channel>
</rss>
