PoshCode Archive  Artifact [66ac924736]

Artifact 66ac9247362739ea9d8e02ef5c7c8bdde3362ff1a93b14abfd5753245e44d936:

  • File List-AD-Users-CSV.ps1 — part of check-in [27c389f37a] at 2018-06-10 13:34:25 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: 4003
# x-archived: 2013-03-10T11:05:10
# x-published: 2013-03-07T13:00: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