Link to home
Start Free TrialLog in
Avatar of gd6627
gd6627Flag for United States of America

asked on

GEt users last logon time

I need a Powershell script that will get me the last logon time of a user
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

If you base it on lastLogonTimeStamp you can use the lastLogondate property.
Get-ADUser -Identity username -Properties lastLogonDate

Open in new window

Did you need the more accurate lastLogon value (which isn't replicated)?
No sense in reinventing the wheel:

Import-Module ActiveDirectory

function Get-ADUserLastLogon([string]$userName)
  $dcs = Get-ADDomainController -Filter {Name -like "*"}
  $time = 0
  foreach($dc in $dcs)
    $hostname = $dc.HostName
    $user = Get-ADUser $userName | Get-ADObject -Properties lastLogon 
    if($user.LastLogon -gt $time) 
      $time = $user.LastLogon
  $dt = [DateTime]::FromFileTime($time)
  Write-Host $username "last logged on at:" $dt }

Get-ADUserLastLogon -UserName SaraDavis

Open in new window

Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gd6627


Thank you