# Get password expiration date from variable

**URL:** <https://forums.powershell.org/t/get-password-expiration-date-from-variable/14953>\
**Category:** PowerShell Help\
**Created:** [September 15, 2020, 11:12am UTC](https://forums.powershell.org/t/get-password-expiration-date-from-variable/14953 "2020-09-15T11:12:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pba1211](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pba1211/32/1421_2.png) [@pba1211](https://forums.powershell.org/u/pba1211)\
**Post date:** [September 15, 2020, 11:12am UTC](https://forums.powershell.org/t/get-password-expiration-date-from-variable/14953/1 "2020-09-15T11:12:11Z")

</div>

Hello,

I know how to find the expiration date of a aduser’s password.

That is…when we want this to know from all of the Ad users.

I am struggling to het it from Just a few users .

These users are in a Variabele like $users.samaccountname for instance.

I need to use $users to get the date there password expires , select their name and e-mail adress.

If you know the good way …

Appreciate IT…

---

<div class="post-metadata">

**Author:** ![adminofthings](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@adminofthings](https://forums.powershell.org/u/adminofthings)\
**Post date:** [September 15, 2020, 12:40pm UTC](https://forums.powershell.org/t/get-password-expiration-date-from-variable/14953/2 "2020-09-15T12:40:58Z")

</div>

I don’t know how `$users` was generated, but if it stores the output of `Get-ADUser`, there is a chance it does not have all of the properties required. That will require doing another `Get-ADUser` with the appropriate properties.

Starting a query from scratch, you could do something like the following:

```
$PasswordAge = 30 # arbitrary but substitute your own domain's password policy
$Users = Get-ADUser -Filter 'Enabled -eq $true -and PasswordNeverExpires -eq $false' -Properties Mail,PasswordLastSet |
    Select-Object Name,Mail,@{n='PasswordExpiration';e={$_.PasswordLastSet.AddDays($PasswordAge)}}
```

If you must start from an already populated `$users` and it does not contain password properties, you must query them again:

```
foreach ($user in $users) {
    Get-ADUser $user.SamAccountName -Properties Mail,PasswordLastSet |
        Select-Object Name,Mail,@{n='PasswordExpiration';e={$_.PasswordLastSet.AddDays($PasswordAge)}}
}
```

Of course, you can combine the two approaches. Performing a query for each user is going to take a long time if the user list is long. It may be more efficient to just query the domain again.

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [September 15, 2020, 12:42pm UTC](https://forums.powershell.org/t/get-password-expiration-date-from-variable/14953/3 "2020-09-15T12:42:33Z")

</div>

Reference the samaccountname as the identity:

```
$csv = @"
SamAccountName
Name1
Name2
Name3
"@ | ConvertFrom-Csv

$results = foreach ($user in $csv) {
    Get-ADUser -Identity $user.SamAccountName -Properties Name, Mail, AccountExpirationDate
}

$results
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/get-password-expiration-date-from-variable/14953/4 "2024-05-16T20:28:55Z")

</div>

Closed on May 16, 2024, 8:28pm UTC


