PoshCode Archive  Artifact [28edff2952]

Artifact 28edff2952cf1776f701b0feed11822887f72bab86d69b902adb8ac833b9c032:

  • File List-AD-Users-CSV.ps1 — part of check-in [d12e67ddc6] at 2018-06-10 13:23:09 on branch trunk — 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. (user: AlphaSun size: 1366)

# encoding: ascii
# api: powershell
# title: List AD Users CSV
# description: 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.
# version: 0.1
# type: class
# author: AlphaSun
# license: CC0
# x-poshcode-id: 3258
# x-derived-from-id: 3474
# x-archived: 2015-09-03T05:22:31
# x-published: 2012-02-28T06:45:00
#
#
$NumDays = 0
$LogDir = ".\User-Accounts.csv"

$currentDate = [System.DateTime]::Now
$currentDateUtc = $currentDate.ToUniversalTime()
$lltstamplimit = $currentDateUtc.AddDays(- $NumDays)
$lltIntLimit = $lltstampLimit.ToFileTime()
$adobjroot = [adsi]''
$objstalesearcher = New-Object System.DirectoryServices.DirectorySearcher($adobjroot)
$objstalesearcher.filter = "(&(objectCategory=person)(objectClass=user)(lastLogonTimeStamp<=" + $lltIntLimit + "))"

$users = $objstalesearcher.findall() | select `
@{e={$_.properties.cn};n='Display Name'},`
@{e={$_.properties.samaccountname};n='Username'},`
@{e={[datetime]::FromFileTimeUtc([int64]$_.properties.lastlogontimestamp[0])};n='Last Logon'},`
@{e={[string]$adspath=$_.properties.adspath;$account=[ADSI]$adspath;$account.psbase.invokeget('AccountDisabled')};n='Account Is Disabled'}

$users | Export-CSV -NoType $LogDir