Find Last Login of All Users in an OU

#Purpose: Find the Active Directory account and last login for every user in an Organizational Unit

$ou = "OU=<obfuscated>,OU=Staff,OU=<obfuscated>,OU=<obfuscated>,DC=<obfuscated>,

DC=<obfuscated>,DC=<obfuscated>"

 

Get-ADUser -Filter * -SearchBase $ou -Properties LastLogon |    

   

Select-Object Name, Enabled, GivenName, Surname, ObjectClass,  

 

@{Name='OU';Expression={($_.DistinguishedName -split ',')[1] -replace '^OU=', ''}},  

 

@{Name='LastLogon';Expression={[DateTime]::FromFileTime($_.LastLogon)}} |

 

Export-Csv "C:\Users\Public\Documents\ADTst.csv" -NoTypeInformation