AD User Last Logon information

Dear All,

Is there any Power Shell script to generate last logon time stamp for a list of users. I got the below script and it is working fine however I sound not export to csv format. Any one can help to modify the script adding export-csv to the below script

# Get a list of every domain controller's name
$dcNames = Get-ADDomainController -Filter * |
Select-Object -ExpandProperty Name |

# Get list of users from Text File
$users = Get-Content "C:\AD-Scripts\users.txt" | Get-ADUser -Properties *

# Hashtable used for splatting for Get-ADUser in loop
$params = @{
"Properties" = "lastLogon"

foreach ( $user in $users ) {
# Set LDAPFilter to find specific user
$params.LDAPFilter = "(sAMAccountName=$($user.SamAccountName))"
# Clear variables
$latestLogonFT = $latestLogonServer = $latestLogon = $null
# Iterate every DC name
foreach ( $dcName in $dcNames ) {
# Query specific DC
$params.Server = $dcName
# Get lastLogon attribute (a file time)
$lastLogonFT = Get-ADUser @params |
Select-Object -ExpandProperty lastLogon
# Remember most recent file time and DC name
if ( $lastLogonFT -and ($lastLogonFT -gt $latestLogonFT) ) {
$latestLogonFT = $lastLogonFT
$latestLogonServer = $dcName
if ( $latestLogonFT -and ($latestLogonFT -gt 0) ) {
# If user ever logged on, get DateTime from file time
$latestLogon = [DateTime]::FromFileTime($latestLogonFT)
else {
# User never logged on
$latestLogon = $latestLogonServer = $null
# Output user
$user | Select-Object `
@{Name = "LatestLogon"; Expression = {$latestLogon}},
@{Name = "LatestLogonServer"; Expression = {$latestLogonServer}}

Could you please go back and fix/edit your post and format your code using the code tags “PRE”? Thanks.

Read Me Before Posting! You’ll be Glad You Did!

Did you try to pipe the output of that script to a Export-Csv?

Fixed the code with PRE tag.

Yes, I have added export-csv but I found only the last user information has been exported.

Great. Thanks.

I did not say to add Export-Csv I asked if you tried already to pipe the output of that script to Export-CSV. What do you usually do with this script? Does it output what you expect? If the answer is yes you can simply pipe the output of that script to Export-CSV. You don’t need to modify it. You could create a new script run this one in the new script and pipe the ouput to Export-CSV in that new script.

If you insist to modify the original script you’d need to collect the output of the foreach loop in a variable and later pipe that variable to Export-Csv.

I tried to pipe the out to Export-CSV but getting below error

At C:\AD-Scripts\Lastlogon.ps1:45 char:3
+ } | export-csv c:\temp\UserList.csv
+   ~
An empty pipe element is not allowed.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : EmptyPipeElement

You told that the script is working fine. How do you run this script? You code snippet still looks like you modified the script. Don’t do that. Just run the script and pipe the output to Export-Csv. What’s so hard to understand on that?

PS C:\> 'Complete Path To Your Perfectly Fine Running Script.ps1' | export-csv c:\temp\UserList.csv