Powershell: AD Group Membership from OU: Difference between revisions
From Mike Beane's Blog
(Created page with "=Purpose= Using Powershell, Build a CSV of group membership from a designated OU. =Code= <pre> ################################################## ## List Groups from Users fo...") |
mNo edit summary |
||
Line 42: | Line 42: | ||
[[Category:Powershell]] | [[Category:Powershell]] | ||
[[Category:Weblog-2019-04]] |
Latest revision as of 13:12, 20 April 2019
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