PoshCode Archive  Artifact [bf862a4167]

Artifact bf862a41672672f17f45338e000dc1bdf2669024a918767f8405bdeb0db431f4:

  • File List-AD-Users-CSV.ps1 — part of check-in [50f660ddd1] at 2018-06-10 13:49: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: 5035
# x-derived-from-id: 5037
# x-archived: 2015-09-03T05:22:37
# x-published: 2015-04-01T17:08: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