Avatar of gd6627
gd6627
Flag 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
Powershell

Avatar of undefined
Last Comment
gd6627

8/22/2022 - Mon
Chris Dent

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)?
Jason Crawford

No sense in reinventing the wheel:

https://technet.microsoft.com/en-us/library/dd378867(v=ws.10).aspx

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

ASKER CERTIFIED SOLUTION
Shaun Vermaak

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gd6627

ASKER
Thank you
Your help has saved me hundreds of hours of internet surfing.
fblack61