PoshCode Archive  Artifact [3efd4c129f]

Artifact 3efd4c129ff568794f60f9e569a1f67cea3de88f2393d92d9387528a4294416c:

  • File List-AD-Users-CSV.ps1 — part of check-in [922aa197fa] at 2018-06-10 13:43:31 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: Jason B size: 1338)

# 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: Jason B
# license: CC0
# x-poshcode-id: 4598
# x-archived: 2013-11-14T15:06:33
# x-published: 2013-11-11T02:55: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