#Alan dot Kaplan at VA dot Gov #10-9-13 #Based on Get_User Last_Logon_v1.1.ps1, by Ahmed MALEK - 3/17/13 #Kaplan overhauled the select of domain and domain controllers #requires -module ActiveDirectory import-module activedirectory cls $adDomain = Get-AdDomain $DNSDomain= $adDomain.dnsroot $dom = $DNSDomain $DNSDomain = Read-host "Search what domain (ENTER for $DNSDomain)" if (!$DNSDomain){$DNSDomain = $dom } $DomainControllers = Get-ADDomainController -server $DNSDomain -Filter {Enabled -eq $true} |Select-Object -Property hostname $samaccountname = Read-Host "What is the user's samaccountname? (Ex: $env:username)" $samAccountName = $samaccountname.ToUpper() #mostly Malek follows "Checking all DCs in $DNSDomain ...`n" foreach ($DomainController in $DomainControllers) { $domaincontroller.hostname + " ... " $UserLastlogon = Get-ADUser -Identity $samaccountname -Properties LastLogon -Server $DomainController.hostName if ($RealUserLastLogon -le [DateTime]::FromFileTime($UserLastlogon.LastLogon)) { $RealUserLastLogon = [DateTime]::FromFileTime($UserLastlogon.LastLogon) $LastusedDC = $DomainController.hostName } } "`n`n" if ($RealUserLastLogon -eq "12/31/1600 19:00:00"){ "$samaccountname has never logged on" } ELSE{ #Kaplan [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') $strOUT = "The last logon for $samaccountname was on $LastusedDC at $RealUserLastLogon" $message = $strout + "`nDo you to copy this information to your clipboard?" $retval = [Microsoft.VisualBasic.Interaction]::MsgBox($Message,'YesNo,systemmodal,Question','Copy') if ($retval -eq 'Yes') { $strOUT | Clip write-host "Done!" } }