Last Logon Time of All Users - RDP server not on a domain

For local users, you can use Powershell with ADSI as follows.

$comp=[adsi]"WinNT://$($env:ComputerName)"
$users = $comp.Children | ?{ $_.SchemaClassName -eq 'User' }
$users | select @{L="Name";E={$_.psbase.Properties.Name.Value}},@{L="LastLogin";E={$_.psbase.Properties.LastLogin.Value}}

I've encountered similar problem a while ago. This was the code that produced the results I needed. Works even for local accounts.

$adsi = [ADSI]"WinNT://$env:COMPUTERNAME" $adsi.Children | where {$_.SchemaClassName -eq 'user'} | ft name,lastlogin

Kudos to Anthony Howell in this discussion.


If you are in a domain, create a powershell script like that;

Import-Module ActiveDirectory

function Get-ADUsersLastLogon()
{
  $dcs = Get-ADDomainController -Filter {Name -like "*"}
  $users = Get-ADUser -Filter *
  $time = 0
  $exportFilePath = "c:\lastLogon.csv"
  $columns = "name,username,datetime"

  Out-File -filepath $exportFilePath -force -InputObject $columns

  foreach($user in $users)
  {
    foreach($dc in $dcs)
    { 
      $hostname = $dc.HostName
      $currentUser = Get-ADUser $user.SamAccountName | Get-ADObject -Server $hostname -Properties lastLogon

      if($currentUser.LastLogon -gt $time) 
      {
        $time = $currentUser.LastLogon
      }
    }

    $dt = [DateTime]::FromFileTime($time)
    $row = $user.Name+","+$user.SamAccountName+","+$dt

    Out-File -filepath $exportFilePath -append -noclobber -InputObject $row

    $time = 0
  }
}

Get-ADUsersLastLogon

Took from there