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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,396 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 98,531 Reputation points MVP
    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

    1 person 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.

    1 person found this answer helpful.
    0 comments No comments