PoshCode Logo PowerShell Code Repository

List AD Users CSV by prueba 5 months ago (modification of post by prueba view diff)
View followups from prueba | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6464"></script>download | new post

This script will list all User objects in the current Active Directory domain. The data gathered includes Display Name, Username, Last Logon Date, and Disabled Status. All data is exported to a CSV file.

  1. $NumDays = 0
  2. $LogDir = ".\sfonseca\Accounts.csv"
  3.  
  4. $currentDate = [System.DateTime]::Now
  5. $currentDateUtc = $currentDate.ToUniversalTime()
  6. $lltstamplimit = $currentDateUtc.AddDays(- $NumDays)
  7. $lltIntLimit = $lltstampLimit.ToFileTime()
  8. $adobjroot = [adsi]''
  9. $objstalesearcher = New-Object System.DirectoryServices.DirectorySearcher($adobjroot)
  10. $objstalesearcher.filter = "(&(objectCategory=person)(objectClass=user)(lastLogonTimeStamp<=" + $lltIntLimit + "))"
  11.  
  12. $users = $objstalesearcher.findall() | select `
  13. @{e={$_.properties.cn};n='Display Name'},`
  14. @{e={$_.properties.samaccountname};n='Username'},`
  15. @{e={[datetime]::FromFileTimeUtc([int64]$_.properties.lastlogontimestamp[0])};n='Last Logon'},`
  16. @{e={[string]$adspath=$_.properties.adspath;$account=[ADSI]$adspath;$account.psbase.invokeget('AccountDisabled')};n='Account Is Disabled'}
  17.  
  18. $users | Export-CSV -NoType $LogDir

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me