Actions

Powershell: AD Group Membership from OU

From Rabbi Blog

Revision as of 13:12, 20 April 2019 by Rabbi Bob (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Purpose

Using Powershell, Build a CSV of group membership from a designated OU.

Code

##################################################
## List Groups from Users found in target $OU ##
## V 0.1
##################################################
## To Do
## - Build Output to CSV [Done]
## - Build CLI variable for OU
###################################################
## Variables (System - Do Not Touch)
$DATE = (Get-Date -UFormat "%Y%m%d-%H%M%S-")
###################################################
## Variables (User)
$DOMAIN = "DC=rabbibob,DC=com"
$OU = "OU=Users"
$csvFileName = "AD_Groups_OUScan_$DATE$OU.csv"
####################################################
## Do Not Edit Below This Line
####################################################
$USERS = @()
$SEARCH_OU = "$OU,$DOMAIN"
write-host $SEARCH_OU
####################################################
# Start Search and array population
####################################################
$GetOU = Get-ADUser -SearchBase $SEARCH_OU -Filter *
$USERS = foreach ($user in $GetOU) {
    $UserDN = $user.DistinguishedName
    $Name=$user.SamAccountName 
    Write-Host "Processing" $Name
    Get-ADGroup -LDAPFilter "(member=$UserDN)" | Select-Object @{Name='User';Expression={$Name}},GroupCategory,GroupScope,Name,ObjectClass,SamAccountName 
    }
####################################################
# Export to $csvFilename
    $users | Export-CSV $csvFileName