Skip to content

Instantly share code, notes, and snippets.

@WilliamBerryiii
Last active December 1, 2015 21:24
Show Gist options
  • Save WilliamBerryiii/40138d8dd7034e10c32d to your computer and use it in GitHub Desktop.
Save WilliamBerryiii/40138d8dd7034e10c32d to your computer and use it in GitHub Desktop.
Active Directory User & Groups Flatten Scan for Powershell V2.0
Try { Import-Module ActiveDirectory -ErrorAction Stop }
Catch { Write-Host "Unable to load Active Directory module."; Break; }
$hostname = [system.environment]::MachineName
$users = Get-ADUser -Filter * -Server $hostname
$users | % {
$user = Get-ADUser "$_" -Properties * -Server $hostname
$Groups = Foreach ($group in $user.MemberOf){
(Get-ADGroup $group -Server $hostname).Name
}
if($groups -ne $null){
$groups = $groups | Sort
$groups = [string]::Join(",",($groups))
} else {
$groups = [string]::Empty
}
$user = New-Object -typename PSObject |
Add-Member -MemberType NoteProperty -Name DistinguishedName -Value $user.DistinguishedName -passthru |
Add-Member -MemberType NoteProperty -Name Enabled -Value $user.Enabled -passthru |
Add-Member -MemberType NoteProperty -Name SamAccountName -Value $user.SamAccountName -passthru |
Add-Member -MemberType NoteProperty -Name EmailAddress -Value $user.EmailAddress -passthru |
Add-Member -MemberType NoteProperty -Name whenChanged -Value (([DateTime]$user.whenChanged) -f 'F') -passthru |
Add-Member -MemberType NoteProperty -Name whenCreated -Value (([DateTime]$user.whenCreated) -f 'F') -passthru |
Add-Member -MemberType NoteProperty -Name Groups -Value $groups -passthru
return $user
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment