Learn how to get lastlogon timestamp for specific OU and export to CSV by using Powershell script.
A PowerShell solution using the AD module cmdlet:
Get-ADUser -Filter * -SearchBase "ou=users,dc=contoso,dc=local" -ResultPageSize 0 -Prop CN,lastLogonTimestamp | Select CN,lastLogonTimestamp | Export-CSV -NoType last.csv
If you want get a date:
Get-ADUser -Filter * -SearchBase "ou=users,dc=contoso,dc=local" -ResultPageSize 0 -Prop CN,lastLogonTimestamp | Select CN,@{n="lastLogonDate";e={[datetime]::FromFileTime($_.lastLogonTimestamp)}} | Export-CSV -NoType last.csv
Below Command works if you want to leverage Quest’s cmdlets:
$OutFile = 'C:\MyExportFile.csv' # your output file$MyOU = 'OU=A,OU=B,OU=C,DC=ChildDomain,DC=ParentDomain,DC=com' # DNGet-QADUser -Searchroot $MyOU | foreach {Add-Content -path $OutFile "$($_.SamAccountName),$($_.LastLogonTimestamp)"}
This will do the same without any extra cmdlets:
$as = [adsisearcher]"(&(objectClass=person)(objectCategory=user))" $as.PropertiesToLoad.Add('cn')$as.PropertiesToLoad.Add('lastlogonTimeStamp')$as.PageSize = 200$as.FindAll() | ForEach-Object { $props = @{ 'CN' = ($_.properties.item('cn') | Out-String).Trim() 'LastLogonTimeStamp' = ([datetime]::FromFiletime(($_.properties.item('lastlogonTimeStamp') | Out-String).Trim())) } New-Object psObject -Property $props } | Export-Csv 'Lastlogontimestamp.csv' -NoTypeInformation
8 thoughts on “Powershell Script to Get “lastLogon Timestamp” for Specific OU and Export to CSV File”
Great post, thanks for share.
Best of luck for the next!
Wow, this article is fastidious.
Keep up the great work.
Many thanks for sharing!
Good job, cheers|
Excellent stuff, just excellent!|
Amazing article dude! Thanks!!
Thanks for providing this info. Very usefull.