Skip to content

Instantly share code, notes, and snippets.

@bohack
Created October 31, 2017 14:10
Show Gist options
  • Select an option

  • Save bohack/e4c6ba83268feffd0dfaf2713c15598b to your computer and use it in GitHub Desktop.

Select an option

Save bohack/e4c6ba83268feffd0dfaf2713c15598b to your computer and use it in GitHub Desktop.
PowerShell Find Last Logon Time
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 server2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment