Check last logged on date from user

Check the last logged on date for users. Fill in the correct filter (e.g. Name, Office) and the name of it (e.g. joe*

 

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 }


foreach ($user in Get-ADUser -Filter 'Name -like "*joe*"' -Properties *) {
    Get-ADUserLastLogon -UserName $user
    }

 

Leave a Reply

Your email address will not be published. Required fields are marked *