Get lastlogon date and time RRS feed

  • Question

  • Hi , Can anyone assist with how I can include the lastlogon date and time for each user with this script. The date format is dd/mm/yyyyy . 

    Any assistance with this would be most appreciated.

    Get-ADUser -Filter * -Properties ou | Select Name, SamAccountName, @{n='OU';e={($_.distinguishedName -Split ",")[1].Replace("OU=", "").Replace("CN=", "")}} | Export-Csv "C:\users1.csv" -Encoding "Unicode"

    Wednesday, June 10, 2020 3:39 AM

All replies

  • Just add it to properties and the select statement.  Why is that hard?

    "OU" is not a property of any account object.


    Wednesday, June 10, 2020 4:11 AM
  • I suppose there is a little bit of conversion to a readable format for LastLogon property:

    Get-ADUser -Filter * -Properties LastLogon | Select Name, SamAccountName, @{n='OU';e={($_.distinguishedName -Split ",")[1].Replace("OU=", "").Replace("CN=", "")}}, @{n="lastLogon"; e={[DateTime]::FromFileTime($_.lastLogon)}} | Export-Csv "C:\users1.csv" -Encoding "Unicode"

    Wednesday, June 10, 2020 4:36 PM
  • Use "LastLogonDate" as it is converted for you by PowerShell.

    Use your search engine to find discussions on how and why.


    Wednesday, June 10, 2020 4:42 PM
  • Hi,
    Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Yang Yang

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, July 3, 2020 3:34 AM