# password last set export csv

**URL:** <https://forums.powershell.org/t/password-last-set-export-csv/9837>\
**Category:** PowerShell Help\
**Created:** [December 10, 2017, 8:00am UTC](https://forums.powershell.org/t/password-last-set-export-csv/9837 "2017-12-10T08:00:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matty21202](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@matty21202](https://forums.powershell.org/u/matty21202)\
**Post date:** [December 10, 2017, 8:00am UTC](https://forums.powershell.org/t/password-last-set-export-csv/9837/1 "2017-12-10T08:00:23Z")

</div>

Hi All,

I have created quick script for password last set

```
$users=Get-ADUser -filter * -Properties name, passwordlastset | Select-Object name, passwordlastset

$users | export-csv "c:\passwordlastset.csv"

$PWLS = $users.passwordlastset

$date = Get-Date

foreach ($user in $users)
{
    if($pwls -lt $date.AddDays(-1))
    {write-output "'$($user.name)'this needs changing"
    }}
```

although how would I export the results of the foreach loop /if statement to a csv? my idea is that once I export the results, I will then import that csv underneath the above code, and start my next bit of the script working off that csv of the affected users.

thanks!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 10, 2017, 8:58am UTC](https://forums.powershell.org/t/password-last-set-export-csv/9837/2 "2017-12-10T08:58:50Z")

</div>

You could de something like this:

```
$Today = Get-Date
Get-ADUser -Filter * -Properties name, passwordlastset -OutVariable ADUserList |
Select-Object -Property Name,
passwordlastset,
@{Name = ‘Change needed’;Expression = {If($_.passwordlastset -lt $Today.AddDays(-1)){$true}Else{$false}}} -OutVariable PWChangeList
```
 (untested)  
This way you have your “results” in the two variables ADUserList and PWChangeList and you could export it or do more things as needed. Maybe like this:
```
$ADUserList | Export-Csv -Path ADUserList.csv -Delimiter ‘,’ -NoTypeInformation
$PWChangeList | Export-Csv -Path PWChangeList.csv -Delimiter ‘,’ -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [December 10, 2017, 4:09pm UTC](https://forums.powershell.org/t/password-last-set-export-csv/9837/3 "2017-12-10T16:09:40Z")

</div>

But..But..

You are going after this data repeatedly / dynamically from ADDS.

Why take the extra steps of serializing dynamic data to the drive, just to read it back in?

IMHO, that is a lot of unnecessary steps and you can simplify this down to a PSCustomObject for easier use / manipulation.  
Again, PoSH is all about discovery, flexibility and choice. Not all of us will agree on the same things.

Anyways, this would be my approach at this use case. Always dynamic, no serialization to disk required, no extra variables.  
Using object properties and as a function for, well, you know.

Function Get-PasswordLastSet  
{  
[CmdletBinding()]  
[alias(‘pwls’)]

```
Param()

Clear-Host
Get-ADUser -filter * -Properties PasswordLastSet |
ForEach-Object {
                    [PSCustomObject]@{
                        Name = $_.Name
                        PassWordLasSet = $_.PasswordLastSet
                        ChangeNeeded = ($_.PasswordLastSet -lt (Get-Date(Date)).AddDays(-1))
                    }
               }

```

}

Get-PasswordLastSet |  
Select -First 2 |  
Format-Table -AutoSize

# Results

Name PassWordLasSet ChangeNeeded

* * *

Administrator 3/31/2017 2:34:34 PM True  
Guest True

But if you really wanted to serialize to disk you could of course.

# Full PWLS Report

Get-PasswordLastSet | Export-Csv -Path “$env:USERPROFILE\Documents\UserPwlsReport.csv” -NoTypeInformation

# Or just the User Name list

Get-PasswordLastSet | Select Name | Export-Csv -Path “$env:USERPROFILE\Documents\UserPwlsReport.csv” -NoTypeInformation

psEdit -filenames “$env:USERPROFILE\Documents\UserPwlsReport.csv”

---

<div class="post-metadata">

**Author:** ![shihanpietersz](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@shihanpietersz](https://forums.powershell.org/u/shihanpietersz)\
**Post date:** [December 10, 2017, 4:21pm UTC](https://forums.powershell.org/t/password-last-set-export-csv/9837/4 "2017-12-10T16:21:16Z")

</div>

by looking at your original post. it seems to look like your trying to create a script or function. you can also try something like this

```
$users = Get-ADUser -filter * -Properties name, passwordlastset | Select-Object name, passwordlastset

$users | export-csv "c:\Scripts\ passwordlastset.csv"

$PWLS = $users.passwordlastset

$date = Get-Date

foreach ($user in $users)
{
    if($pwls -lt $date.AddDays(-1)){
        $PWCUsers = [pscustomobject]@{
                                "User" = $user.name
                                "PasswordLastSet" = $user.passwordlastset
                                }
         $PWCUsers
         $PWCUsers | Export-Csv -NoTypeInformation C:\scripts\temp.csv -Force -Append
    }#IF

}#Foreach
```

---

<div class="post-metadata">

**Author:** ![matty21202](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@matty21202](https://forums.powershell.org/u/matty21202)\
**Post date:** [December 11, 2017, 4:04pm UTC](https://forums.powershell.org/t/password-last-set-export-csv/9837/5 "2017-12-11T16:04:48Z")

</div>

Thanks shihan, this is helpful!

---

<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:37pm UTC](https://forums.powershell.org/t/password-last-set-export-csv/9837/6 "2024-05-16T20:37:03Z")

</div>

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


