Password Expire Report – Powershell

Hello! welcome to another interesting task i was working on couple of days ago. To give a bit of background of what exactly i was doing.

Part of My Domain to Domain Migration (AKA ADMT Migration) project i was asked to generate a report which basically pulls all of users from the source domain and check the password to check how many users have password expiring sooner. It was a fun task to do, i just added bit of a HTML touch to make it look like an actual report, (Management loves those reports).

Report shows password that expires in a day,10 and 20 days. You get 3 reports also you can modify the script to match your needs.

Less talking and to the point, Script. Please change the below script for your environment.

$useridentified = Get-ADUser -Filter {((enabled -eq $true) } -SearchBase "OU=OUName,DC=domainname,DC=domainname" -Properties givenname,surname,samaccountname,mail,PasswordLastSet | select givenname,surname,samaccountname,mail,PasswordLastSet
$userforpasswordcheck = @()
$Reportforpassword = @()
$10days = @()
$20days = @()
$1day = @()

foreach($Suser in $useridentified){

$userforpasswordcheck += Get-ADUser $Suser.SamAccountName -Properties givenname,surname,samaccountname,mail,PasswordLastSet -Server Servername | select givenname,surname,samaccountname,mail,PasswordLastSet

}

foreach($SingleUser in $userforpasswordcheck){
try{
$NextReset = $SingleUser.PasswordLastSet.AddDays(60)
}catch{
}

   $Passwordreport = New-Object -TypeName psobject -Property @{
    "Firstname" = $SingleUser.givenname
    "Lastname" = $SingleUser.surname
    "Username" = $SingleUser.samaccountname
    "Email" = $SingleUser.mail
    "PasswordLastReseton" = ($SingleUser.PasswordLastSet).Date
    "Expiry" = $NextReset 
    "Daysremaining" = ($NextReset - (Get-Date)).Days
   }

      $Reportforpassword += $PasswordReport | Select-Object  "Firstname","Lastname","Username","Email","PasswordLastReseton","Expiry","Daysremaining"
}

foreach($line in $Reportforpassword){
    if(($line.Daysremaining -eq 1) -or $line.Daysremaining -lt 1){
        $1day += $line
    }elseif($line.Daysremaining -lt 10){
        $10days += $line
    }elseif($line.Daysremaining -lt 20){
        $20days += $line
    }
}

   $Style = @"
<style>
BODY{font-family:Calibri;font-size:10pt;}
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse; padding-right:5px}
TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;color:black;background-color:#FFFFFF }
TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:orange}
TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black}
</style>
"@

$reportpath = 'C:\PasswordEmails\HTML\today.html'

$20days | Select-Object "Firstname","Lastname","Username","Email","PasswordLastReseton","Expiry","Daysremaining" | Sort-Object Daysremaining | ConvertTo-Html -body "<H2> Users Password Check less than 20 days</H2>"-Head $style | Out-File 'C:\PasswordEmails\HTML\20days.html'

$10days | Select-Object "Firstname","Lastname","Username","Email","PasswordLastReseton","Expiry","Daysremaining" | Sort-Object Daysremaining | ConvertTo-Html -body "<H2>Users Password Check less than 10 days</H2>"-Head $style | Out-File 'C:\PasswordEmails\HTML\10days.html'

$1day | Select-Object "Firstname","Lastname","Username","Email","PasswordLastReseton","Expiry","Daysremaining" | Sort-Object Daysremaining | ConvertTo-Html -body "<H2>Users Password Check less than 1 days</H2>"-Head $style | Out-File 'C:\PasswordEmails\HTML\1day.html'

#$Reportforpassword | Select-Object "Firstname","Lastname","Username","Email","PasswordLastReseton","Expiry","Daysremaining" | Sort-Object Daysremaining | ConvertTo-Html -body "<H2>Users Password Check</H2>"-Head $style | Out-File $reportpath

Report will look like as below. Obviously i have to blur the fields. (you know why :P)

Have a nice day!

Published by iamfazul

Author of the site

2 thoughts on “Password Expire Report – Powershell

Leave a comment