I'm attempting to create a report to view all users on my system who are currently enabled and disabled along with showing me their last logon time.

I have two different scripts which do each job, however, i'm attempting to merge them into the same script so I can view all this information at the same time but running into issues.

This is my current attempt at gathering information:

get-aduser -Filter * -Properties CN,samaccountname,AccountExpirationDate,Enabled,lastLogonTimestamp | Select CN,samaccountname,AccountExpirationDate,Enabled,@{n="lastLogonDate";e={[datetime]::FromFileTime,
($_.lastLogonTimestamp)}} |sort Enabled | export-csv "c:\lemon.csv"

But the result is I get the last login displaying as static datetime FromFileTime(long fileTime)

what would be the best method to pull last logon date which allows to pull this information in one report?

1 Answers

Matthew On Best Solutions

I managed to modify a script I already had into the following:

Import-Module ActiveDirectory

Get-ADUser -Filter * -SearchBase "DC=x,DC=x,DC=x" -ResultPageSize 0 -Property CN,Description, Enabled, LastLogonTimestamp |
    Select-Object -Property CN, Description,Enabled, @{ n = "LastLogonDate"; e = { [datetime]::FromFileTime( $_.lastLogonTimestamp ) } } |
    Sort-Object -Property CN, Description, Enabled, LastLogonDate |
    Export-CSV -NoTypeInformation "C:\Users\x\Desktop\results2.csv"

This code has resolved the issue, however, still unsure why my original code wasn't completing correctly.