Using Powershell To Get User Last Logon Date

Home / Using Powershell To Get User Last Logon Date
Share This:

As an Active Directory Administrator, determining the date that a user last logged onto the network could be important at some point. If you have access to the Attribute Editor in your Active Directory tools, you can look for the LastLogonDate attribute. The other option is to use Powershell, and there are two methods to access this information.

Using Get-ADUser

The first option basically gives you the same data that the Attribute Editor GUI would display. In Powershell, run this command to get the data you need, then scroll down the list and look for LastLogonDate.

Get-ADUser username -properties *

Powershell Script

The next method is to use the Powershell script below. Save this script as a .ps1 file and edit the username in the last line of the script (in bold below), then run it.

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 username

By | 2017-08-17T11:47:25+00:00 August 17th, 2017|Categories: Windows|Tags: , , , , |0 Comments