none
Retrieve AD last logon in 'human readable' format?

    Question

  • Hi All,

    I am trying to retrieve the last logon time for an AD Account in human readable format (of year-month-day).

    In know that I can retrieve the last logon using the code below -

    Get-ADUser fred.jones | Get-ADObject -Properties lastLogon

    Furthermore I believe that I should be able to use the ToString("yyyy-mm-dd") command to transform the format of the results from the above code.

    However I am stuck with regard to 'stitching' things together.

    Can anybody offer any advice here?

    Kind Regards,

    Davo


    Monday, March 13, 2017 11:14 PM

Answers

  • If you do decide to use lastLogon, this is how you convert to datetime:

    Get-ADUser -Identity fred.jones -Properties LastLogon | 
        select Name, @{Name='LastLogon';Expression={[DateTime]::FromFileTime($_.LastLogon)}}

    It looks like you got the help you needed from Richard and jrv. Please remember to mark an answer. That will help other users who is searching for the same solution, and it will prevent contributors from wasting their time on already solved questions.

    • Marked as answer by Darts75 Tuesday, March 14, 2017 9:17 PM
    Tuesday, March 14, 2017 8:10 PM

All replies

  • First, no need to pipe the results of Get-ADUser to Get-ADObject. The Get-ADUser cmdlet can retrieve lastLogon just as easily. But if you do retrieve lastLogon, you should query all DC's and specify each with the -Server parameter, as the value is not replicated. Each value only makes sense with regard to one DC. Finally, retrieve the LastLogonDate PowerShell property instead. It is based on the lastLogonTimeStamp attribute, is replicated to all DC's and is retrieved in "friendly" format, in the local time zone. This value should be accurate within about 14 days, and is satisfactory for most purposes.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Monday, March 13, 2017 11:24 PM
  • Get-ADUser fred.jones -Properties lastLogonDATE

    \_(ツ)_/


    Tuesday, March 14, 2017 1:08 AM
    Moderator
  • Thanks guys :)
    Tuesday, March 14, 2017 2:13 AM
  • If you do decide to use lastLogon, this is how you convert to datetime:

    Get-ADUser -Identity fred.jones -Properties LastLogon | 
        select Name, @{Name='LastLogon';Expression={[DateTime]::FromFileTime($_.LastLogon)}}

    It looks like you got the help you needed from Richard and jrv. Please remember to mark an answer. That will help other users who is searching for the same solution, and it will prevent contributors from wasting their time on already solved questions.

    • Marked as answer by Darts75 Tuesday, March 14, 2017 9:17 PM
    Tuesday, March 14, 2017 8:10 PM
  • Dear Team,

    Should we get this report for all users. Pls help to arrange query.

    Thanks in advance!!!

    Thursday, June 14, 2018 1:45 PM
  • Dear Team,

    Should we get this report for all users. Pls help to arrange query.

    Thanks in advance!!!


    Topic is closed.  If you have a new question please start your own topic.

    \_(ツ)_/

    Thursday, June 14, 2018 2:02 PM
    Moderator