Share via

Get the list of the last logon users from a txt file using powershell

joaomanoelc 171 Reputation points
2021-02-02T12:58:00.817+00:00

I want to get lastlgon from a list in the txt file

My query on powershell gets the result for an account within the file, but with more users an error is generated.

$file = Get-Content -Path "C:\Temp\UsersLastLogon.txt"
Get-ADUser -Identity $file -Properties LastLogonDate | Select-Object SamAccountName, LastLogonDate

63037-cannot-convert-system-object.png

63062-list-lastlogonusers.png

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Andreas Baumgarten 132.1K Reputation points MVP Volunteer Moderator
2021-02-02T14:27:20.633+00:00

Maybe this helps:

$file = Get-Content -Path "C:\Temp\UsersLastLogon.txt"
foreach ($user in $file)
    {
    Get-ADUser -Identity $user -Properties LastLogonDate | Select-Object SamAccountName, LastLogonDate
    }

(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

Was this answer helpful?

2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Marcus 6 Reputation points
    2022-02-18T15:16:22.637+00:00

    hi Andreas, how can you export it to csv file.

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.