2

Very generic question regarding different output for Net User, and Get-ADUser.

What is really going on "under the hood" when I run each command? I bring this up only due to getting different results with the LastLogonDate for any user account running both commands. For example:

  1. Get-ADUser -Identity "Abe" -Properties LastLogonDate ( returns 2/08/2021 )

  2. Net user "Abe" /Domain ( returns 2/11/2021 )

So, what's really going on? I tried to use ADSI but, it returns the same info as Get-ADUser. If this is completely off-topic from what SO is meant for, feel free to close it and I will ask elsewhere.

Thank you!

1 Answer 1

4

The lastLogonDate property returned by Get-ADUser maps to the lastLogonTimestamp attribute in AD which is replicated but not kept completely accurate. See the description here.

Net User refers to the AD attribute lastLogon. Which is accurate but isn't replicated between domain controllers reference Quote:

Please note that this value is NOT replicated between domain controllers - if you want to know the exact last logon time for an account in a domain with more than one domain controllers, you have to check this value on all domain controllers!

There are many scripts out there that collect the domain controllers loop them and return the newest of the values found. You can check this answer and discussion for some insights. Note that we were troubleshooting performance issues with the script but there are code samples nevertheless.

Of particular interest, and not mentioned in the other answer, both of these attributes are stored as 64 bit integers representing the number of 100 nanosecond intervals since 1/1/1601 12:00. However, Get-ADUser doesn't convert lastLogon to a typical [DateTime] value for you. Moreover, it's stored as UTC and must be converted to local time, code similar to below demonstrates the point:

Get-ADUser saporito -properties 'LastLogonDate','LastLogon' | 
Select *,@{Name = 'logonDate'; Expression = { [DateTime]::FromFileTimeUtc( $_.LastLogon ).ToLocalTime()}}

Note: Net User appears to do the same conversion.

Note: I added a property rather than overwrite it. This is for demonstration may need to be adapter to your larger project.

4
  • I'm not sure about that. I actually had already looped through each dc and got the last logon for the same super and they all matched. That was actually what prompted me to ask this question.
    – Abraham Zinala
    Commented Feb 13, 2021 at 0:08
  • 1
    I think I figured it out. You may be looking at the wrong attribute. I updated the answer, let me know how it goes.
    – Steven
    Commented Feb 13, 2021 at 1:00
  • and...... that was it! I took a look at the links as well to help me understand it better. Queried all DC's for my lastlogon and sure enough, I found the same date/time in one of the ones DC's that matched my Net User output. Commented Feb 16, 2021 at 15:39
  • 1
    I'm glad it worked out. Super interesting topic.
    – Steven
    Commented Feb 16, 2021 at 15:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .