Avatar of Parity123
Parity123
Flag for United States of America asked on

Powershell: Password report

Hello,

I have the following code to get the password expiration report. I need assistance in modifying the code to get just the total counts of users password expiring on a specific date. For instance: 01/01/2016

Import-Module ActiveDirectory
Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False -and physicalDeliveryOfficeName -like "*" } `
–Properties "SamAccountName","mail","pwdLastSet","physicaldeliveryofficename","msDS-UserPasswordExpiryTimeComputed" |
Select-Object -Property "SamAccountName","mail","physicaldeliveryofficename",@{Name="Password Last Set";`
Expression={[datetime]::FromFileTime($_."pwdLastSet")}}, @{Name="Password Expiry Date";`
Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}} |
 Export-CSV "C:\PasswordExpirationReport.csv" -NoTypeInformation -Encoding UTF8
Powershell

Avatar of undefined
Last Comment
Parity123

8/22/2022 - Mon
Jason Crawford

So you're going to specify the date when calling the function, and it should in turn return the number of days before the password expires based on the date you specify?
Parity123

ASKER
I can specify the date with a variable in the code.
$mydate="01/01/2016"

and I want the total count of users password expiring on this date.
Jason Crawford

should that include all passwords that have already expired or just passwords expiring on that exact date?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Parity123

ASKER
just on that exact date.
Jason Crawford

This is what I have so far.  It's late and I'm beat so I'll pick it up again tomorrow.  Feel free to modify as needed, but honestly I feel like I'm off track on this one:

[datetime]$date = Read-Host 'Enter date'
$expiry = Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False} –Properties 'DisplayName', 'msDS-UserPasswordExpiryTimeComputed' | Select-Object -Property 'DisplayName',@{n='ExpirationDate';e={[datetime]::FromFileTime($_.'msDS-UserPasswordExpiryTimeComputed')}}

foreach ($i in $expiry) {
    if ($i.ExpirationDate -eq $date) {
        Write-Host $i
    }
}

Open in new window

Parity123

ASKER
Thanks Jason. I tried.

For instance: The value for $i.ExpirationDate is 1/5/2016 9:50:54 AM.

I set the variable $mydate="1/5/2016" just to try

and I changed the code to if ($i.ExpirationDate -contains $mydate) and it does not return anything.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Jason Crawford

Yea I tried to be as upfront as I could that it was an untested script.  I just wanted to put something out there we can use to build on.  I have three Exchange migrations I'm working on at the moment and I just couldn't look at my computer any longer last night.  I'll edit the script and have an updated version to you by the end of the day.
ASKER CERTIFIED SOLUTION
Jason Crawford

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Parity123

ASKER
I tried and does not return any values. some if the values are

12/22/2015 10:35:14 AM
1/9/2016 4:30:10 PM
12/26/2015 8:46:00 AM
2/7/2016 1:31:40 PM
2/5/2016 10:11:07 AM
12/26/2015 12:08:11 AM

I think the issue is comparison. I even hardcoded the date to $mydate="2/5/2016", and used -contains $mydate and does not return values.
Jason Crawford

That's odd we're seeing different results.  I'll keep working on it today.  What PowerShell version are you using?  Just run this command to check:

$PSVersionTable.PSVersion
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Parity123

ASKER
V4.0
Parity123

ASKER
Can someone please assist. Thanks.