Created
October 31, 2017 14:10
-
-
Save bohack/e4c6ba83268feffd0dfaf2713c15598b to your computer and use it in GitHub Desktop.
PowerShell Find Last Logon Time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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