Created
October 28, 2019 21:22
-
-
Save spy86/fbe015a79a4efd303a7f540aed8eb7a4 to your computer and use it in GitHub Desktop.
Get Active Directory user account last logged on 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
| #Check if the account exists. | |
| If($Results.Count -eq 0) | |
| { | |
| Write-Warning "The SamAccountName '$UserName' cannot find. Please make sure that it exists." | |
| } | |
| Else | |
| { | |
| Foreach($Result in $Results) | |
| { | |
| $DistinguishedName = $Result.Properties.Item("DistinguishedName") | |
| $LastLogonTimeStamp = $Result.Properties.Item("LastLogonTimeStamp") | |
| If ($LastLogonTimeStamp.Count -eq 0) | |
| { | |
| $Time = [DateTime]0 | |
| } | |
| Else | |
| { | |
| $Time = [DateTime]$LastLogonTimeStamp.Item(0) | |
| } | |
| If ($LastLogonTimeStamp -eq 0) | |
| { | |
| $LastLogon = $Time.AddYears(1600) | |
| } | |
| Else | |
| { | |
| $LastLogon = $Time.AddYears(1600).ToLocalTime() | |
| } | |
| #Output in comma delimited format. | |
| $Hash = @{ | |
| SamAccountName = $UserName | |
| LastLogonTimeStamp = $(If($LastLogon -match "12/31/1600") | |
| { | |
| "Never Logon" | |
| } | |
| Else | |
| { | |
| $LastLogon | |
| }) | |
| } | |
| $Objs = New-Object -TypeName PSObject -Property $Hash | |
| $Objs | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment