PoshCode Archive  Artifact [a63c528167]

Artifact a63c52816719d0bdf05597e652c631d73a714e3ef6e62073d2005645d4b29c41:

  • File List-AD-Users-CSV.ps1 — part of check-in [4befa54ee0] at 2018-06-10 13:23:08 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: 1339)

# 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: 3257
# x-archived: 2015-09-03T05:22:26
# x-published: 2012-02-27T08:27: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