I created a Powershell script to get the last logon information for domain users. The script works and produces the CSV output, however, it is showing the incorrect time for all users, and it is the same time for all users.<\/p>\n

Advertisement

My script:<\/p>\n

Get-ADUser -Filter {enabled -eq $true} -Properties LastLogonTimeStamp |\nSelect-Object Name, @{Name=\"Stamp\"; Expression=\n{[DateTime]::FromFileTime($_.lastLogonTimestamp).ToString('yyyy-MM-dd_hh:mm:ss')}} | export-csv -Path .\\.Users-Last-Logon.csv\n<\/code><\/pre>\n

Advertisement

My timestamp for every user, including my admin account:<\/p>\n

1600-12-31_07:00:00<\/p>\n

Does anyone have any answers?<\/p>\n

@Microsoft<\/a><\/p>","upvoteCount":3,"answerCount":13,"datePublished":"2022-03-25T15:57:24.000Z","author":{"@type":"Person","name":"phonysoprano","url":"https://community.spiceworks.com/u/phonysoprano"},"acceptedAnswer":{"@type":"Answer","text":"

\n
\n
<\/div>\n\"\" PhonySoprano:<\/div>\n
\n

I updated the script to include lastlogon. Service accounts show me the correct value, but other AD accounts still show the same. I look at the attributes individually and the attribute shows a recent logon. Why isn’t Powershell pulling these attributes correctly?<\/p>\n<\/blockquote>\n<\/aside>\n

Which attributes show a recent date? Do you have a screenshot? Do you run powershell as admin?<\/p>","upvoteCount":2,"datePublished":"2022-03-25T17:48:45.000Z","url":"https://community.spiceworks.com/t/last-logon-script-not-showing-correct-time/828760/12","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"

I created a Powershell script to get the last logon information for domain users. The script works and produces the CSV output, however, it is showing the incorrect time for all users, and it is the same time for all users.<\/p>\n

My script:<\/p>\n

Get-ADUser -Filter {enabled -eq $true} -Properties LastLogonTimeStamp |\nSelect-Object Name, @{Name=\"Stamp\"; Expression=\n{[DateTime]::FromFileTime($_.lastLogonTimestamp).ToString('yyyy-MM-dd_hh:mm:ss')}} | export-csv -Path .\\.Users-Last-Logon.csv\n<\/code><\/pre>\n

My timestamp for every user, including my admin account:<\/p>\n

1600-12-31_07:00:00<\/p>\n

Does anyone have any answers?<\/p>\n

@Microsoft<\/a><\/p>","upvoteCount":3,"datePublished":"2022-03-25T15:57:24.000Z","url":"https://community.spiceworks.com/t/last-logon-script-not-showing-correct-time/828760/1","author":{"@type":"Person","name":"phonysoprano","url":"https://community.spiceworks.com/u/phonysoprano"}},{"@type":"Answer","text":"

If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n

\n
\n
<\/div>\n \"\"\n PLEASE READ BEFORE POSTING! Read if you're new to the PowerShell forum!<\/a> Programming & Development<\/span><\/span><\/a>\n <\/div>\n
\n Hi, and welcome to the PowerShell forum! \n\n\nDon’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask! \n\n\nUse a descriptive subject. Don’t say “Need help” or “PowerShell Help”, actually summarize what the problem is. It helps the rest of us keep track of which problem is which. \n\n\nDon’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy…\n <\/blockquote>\n<\/aside>\n\n

\"192033ab-bb8f-4032-88a5-8e2313af0344-codebutton_small.png\"<\/p>\n

    \n
  • Neally Bot<\/li>\n<\/ul>","upvoteCount":0,"datePublished":"2022-03-25T15:59:10.000Z","url":"https://community.spiceworks.com/t/last-logon-script-not-showing-correct-time/828760/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

    Do you have multiple DCs? This attribute doesn’t sync between DCs, so you’ll need to query them all and parse the data to find the actual time.<\/p>\n

    1600-12-31_07:00:00 would signify that the account hasn’t authenticated against whichever DC you’re querying, as it is “null” in timestamp format.<\/p>","upvoteCount":1,"datePublished":"2022-03-25T16:07:02.000Z","url":"https://community.spiceworks.com/t/last-logon-script-not-showing-correct-time/828760/3","author":{"@type":"Person","name":"big-green-man","url":"https://community.spiceworks.com/u/big-green-man"}},{"@type":"Answer","text":"

    There is one DC.<\/p>","upvoteCount":0,"datePublished":"2022-03-25T16:07:58.000Z","url":"https://community.spiceworks.com/t/last-logon-script-not-showing-correct-time/828760/4","author":{"@type":"Person","name":"phonysoprano","url":"https://community.spiceworks.com/u/phonysoprano"}},{"@type":"Answer","text":"

    \n
    \n
    <\/div>\n\"\" Big Green Man:<\/div>\n
    \n

    Do you have multiple DCs? This attribute doesn’t sync between DCs, so you’ll need to query them all and parse the data to find the actual time.<\/p>\n

    1600-12-31_07:00:00 would signify that the account hasn’t authenticated against whichever DC you’re querying, as it is “null” in timestamp format.<\/p>\n<\/blockquote>\n<\/aside>\n

    lastLogonTimestamp DOES sync. ‘LastLogon’ does not.<\/p>\n

    \n
    \n\n learn.microsoft.com<\/a>\n <\/header>\n\n
    \n
    <\/div>\n\n

    Understanding the AD Account attributes - LastLogon, LastLogonTimeStamp and...<\/a><\/h3>\n\n\n\n <\/article>\n\n
    \n \n \n <\/div>\n\n
    <\/div>\n<\/aside>\n","upvoteCount":1,"datePublished":"2022-03-25T16:09:19.000Z","url":"https://community.spiceworks.com/t/last-logon-script-not-showing-correct-time/828760/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
    \n
    \n
    <\/div>\n\"\" Neally:<\/div>\n
    \n
    \n
    \n
    <\/div>\n\"\" Big Green Man:<\/div>\n
    \n

    Do you have multiple DCs? This attribute doesn’t sync between DCs, so you’ll need to query them all and parse the data to find the actual time.<\/p>\n

    1600-12-31_07:00:00 would signify that the account hasn’t authenticated against whichever DC you’re querying, as it is “null” in timestamp format.<\/p>\n<\/blockquote>\n<\/aside>\n

    lastLogonTimestamp DOES sync. ‘LastLogon’ does not.<\/p>\n

    Understanding the AD Account attributes - LastLogon, LastLogonTimeStamp and LastLogonDate | Microsoft Learn<\/a><\/p>\n<\/blockquote>\n<\/aside>\n

    Oops. Thanks for the correction!<\/p>","upvoteCount":1,"datePublished":"2022-03-25T16:10:25.000Z","url":"https://community.spiceworks.com/t/last-logon-script-not-showing-correct-time/828760/6","author":{"@type":"Person","name":"big-green-man","url":"https://community.spiceworks.com/u/big-green-man"}},{"@type":"Answer","text":"

    \n
    \n
    <\/div>\n\"\" Big Green Man:<\/div>\n
    \n

    1600-12-31_07:00:00 would signify that the account hasn’t authenticated against whichever DC you’re querying, as it is “null” in timestamp format.<\/p>\n<\/blockquote>\n<\/aside>\n

    Correct!<\/p>\n

    That means the lastLogonTimestamp attribute is null (empty) for those accounts.
    \nThe “zero” date for LargeInteger datetime values is 12:00 am Jan. 1 1601, in UTC (Coordinated Universal Time).<\/p>\n

    So those users never logged in.<\/p>","upvoteCount":1,"datePublished":"2022-03-25T16:11:07.000Z","url":"https://community.spiceworks.com/t/last-logon-script-not-showing-correct-time/828760/7","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

    Also different events update different times stamps.<\/p>\n

    I usually check lastlogon AND LastLogonTimeStamp<\/p>\n

    Not ALL event cause lastlogon to update and not ALL events cause LastLogonTimeStamp to replicate (with a up to 14 days delay anyways)<\/p>\n

    Get-ADUser -Filter \"enabled -eq 'true'\" -Properties LastLogonTimeStamp,lastlogon |\nSelect-Object Name,\n@{n=\"LastLogonTimeStamp\"; e={[DateTime]::FromFileTime($_.lastLogonTimestamp)}},\n@{n=\"lastlogon\"; e={[DateTime]::FromFileTime($_.lastlogon)}}\n<\/code><\/pre>\n

    \"cd2485f1-22ae-4d1f-a54d-fb8bd63a1661-2022-03-25_10-37-33.png\"<\/p>","upvoteCount":0,"datePublished":"2022-03-25T16:38:42.000Z","url":"https://community.spiceworks.com/t/last-logon-script-not-showing-correct-time/828760/8","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

    \n
    \n
    <\/div>\n\"\" Neally:<\/div>\n
    \n

    Also different events update different times stamps.<\/p>\n

    I usually check lastlogon AND LastLogonTimeStamp<\/p>\n

    Not ALL event cause lastlogon to update and not ALL events cause LastLogonTimeStamp to replicate (with a up to 14 days delay anyways)<\/p>\n

    Get-ADUser -Filter \"enabled -eq 'true'\" -Properties LastLogonTimeStamp,lastlogon |\nSelect-Object Name,\n@{n=\"LastLogonTimeStamp\"; e={[DateTime]::FromFileTime($_.lastLogonTimestamp)}},\n@{n=\"lastlogon\"; e={[DateTime]::FromFileTime($_.lastlogon)}}\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\n

    Is Seymour part of your AD?<\/p>","upvoteCount":0,"datePublished":"2022-03-25T16:41:42.000Z","url":"https://community.spiceworks.com/t/last-logon-script-not-showing-correct-time/828760/9","author":{"@type":"Person","name":"big-green-man","url":"https://community.spiceworks.com/u/big-green-man"}},{"@type":"Answer","text":"

    \n
    \n
    <\/div>\n\"\" Big Green Man:<\/div>\n
    \n

    Is Seymour part of your AD?<\/p>\n<\/blockquote>\n<\/aside>\n

    Maybe…<\/p>\n

    \"c0f0ef54-2bc2-4b7e-ad10-778a4722f51e-2022-03-25_10-45-16.png\"<\/p>","upvoteCount":1,"datePublished":"2022-03-25T16:46:00.000Z","url":"https://community.spiceworks.com/t/last-logon-script-not-showing-correct-time/828760/10","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

    I updated the script to include lastlogon. Service accounts show me the correct value, but other AD accounts still show the same. I look at the attributes individually and the attribute shows a recent logon. Why isn’t Powershell pulling these attributes correctly?<\/p>","upvoteCount":0,"datePublished":"2022-03-25T17:41:56.000Z","url":"https://community.spiceworks.com/t/last-logon-script-not-showing-correct-time/828760/11","author":{"@type":"Person","name":"phonysoprano","url":"https://community.spiceworks.com/u/phonysoprano"}},{"@type":"Answer","text":"

    I have been beating my head over this. I thought I had set the script to run as admin, apparently I didn’t. That’s all it took. Thank you.<\/p>","upvoteCount":0,"datePublished":"2022-03-25T17:55:43.000Z","url":"https://community.spiceworks.com/t/last-logon-script-not-showing-correct-time/828760/13","author":{"@type":"Person","name":"phonysoprano","url":"https://community.spiceworks.com/u/phonysoprano"}}]}}