Get-ADUser -Filter * -SearchBase "CN=Users,DC=contoso,DC=com" -ResultPageSize 0 -Property CN, LastLogonTimestamp |
Select-Object -Property CN, SamAccountName, @{ n = "LastLogonDate"; e = { [datetime]::FromFileTime( $_.lastLogonTimestamp ) } } |
Sort-Object -Property LastLogonDate
Gets the Active Directory users in a given OU and sorts them by Last Logon Date.
PS C:\> Get-ADUser -Filter * -SearchBase "CN=Users,DC=contoso,DC=com" -ResultPageSize 0 -Property CN, LastLogonTimestamp | >> Select-Object -Property CN, SamAccountName, @{ n = "LastLogonDate"; e = { [datetime]::FromFileTime( $_.lastLogonTimestamp ) } } | >> Sort-Object -Property LastLogonDate CN SamAccountName LastLogonDate -- -------------- ------------- Buddy Guy bguy 11/28/2012 7:54:47 AM Mike Dexter MDexter 11/28/2012 8:48:02 PM Bill Gates BGates 4/9/2013 1:43:58 PM Carl Sagan CSagan 2/6/2014 2:38:04 PM Bill Bryson BBryson 12/11/2015 2:05:54 PM Carl S. Robot crobot 2/16/2016 1:17:25 PM Mike Ness mness 10/3/2018 3:42:13 PM Administrator Administrator 10/26/2018 1:19:51 PM