• 512-991-7197
  • This email address is being protected from spambots. You need JavaScript enabled to view it.
  • Always Open

Welcome to Active Directory Tips & Tricks

Powershell Script to Get “lastLogon Timestamp” for Specific OU and Export to CSV File


Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

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



Recommend other relevant articles:

 

 

© 2019 Austin Laptop [AustinLaptop.Com]. All Rights Reserved.