Thursday, February 13, 2014

Powershell: Get All AD Group Members


Purpose: This script requires the RSAT tools to work. It connects to AD and gets all groups and users within each group and outputs to a csv file. It can also get single groups and display/exports the results.


###################################
#Tony Unger - Get Group members
#12/12/2013
#1.0
###################################

Import-Module activedirectory

do {
  cls
$response = "N"
$ExportPath = "c:\temp\AD_GroupMemberofQuery.csv"
$myCol = @()

[int]$xMenuChoiceA = 0
while ( $xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 4 ){
Write-host "Active Directory Group Member Reporting" -foregroundcolor "magenta"
Write-host "1. Specifiy a Group"
Write-host "2. All Groups"
Write-host "3. Quit and exit"
[Int]$xMenuChoiceA = read-host "Please enter an option 1 to 3..." }

Switch( $xMenuChoiceA ){
  1{$ADGroups = read-host "Please enter the AD group name:"}
  2{$ADGroups = Get-ADGroup -filter {GroupCategory -eq "Security" -and GroupScope -eq "Global"} | Select -expand SamAccountName}
  3{exit}
default{exit}
}

$i = 0
foreach ($ADGroup in $ADGroups){
  

 $i++
 Write-Progress -Activity "Gathering members" -status "Currently on group $ADGroup" -percentComplete ($i / $ADGroups.count*100)
 $Members = get-adgroupmember $ADGroup -recursive

 
 foreach ($MemberofGroup in $Members){
 
  $Detail = New-Object PSObject 
  $Detail | Add-Member Noteproperty GroupName $ADGroup
  $Detail | Add-Member Noteproperty User $MemberofGroup.Name
  $Detail | Add-Member Noteproperty Account_Name $MemberofGroup.SamAccountName
  $myCol += $Detail
 }

}



$myCol | Export-Csv -Path $ExportPath -notype
Write-Host "File exported to: $ExportPath"


[int]$xMenuChoiceB = 0
while ( $xMenuChoiceB -lt 1 -or $xMenuChoiceA -gt 4 ){
cls
Write-host "Active Directory Group Member Reporting" -foregroundcolor "magenta"
Write-host "1. Display current results"
Write-host "2. Query another group"
Write-host "3. Quit and exit"
[Int]$xMenuChoiceB = read-host "Please enter an option 1 to 3..." }

Switch( $xMenuChoiceB ){
  1{$myCol}
  2{$response = "Y"}
  3{exit}
default{exit}
}

}
while ($response -eq "Y")


1 comment:

  1. Merkur & Ferencia: Merkur & Ferencia Merkur
    Merkur & Ferencia merkur - Merkur https://febcasino.com/review/merit-casino/ & Ferencia Merkur in Solingen, Germany - Merkur kadangpintar - sol.edu.kg Merkur Merkur - MERKUR septcasino - Merkur & 출장안마 Ferencia Merkur

    ReplyDelete