Nov 09 2022 02:45 AM
Hi,
I would like to export in a csv file all groups and nested groups from bulk AD user or OU.
Example:
Users | Groups | NestedGroups | |||||||||
User1 | Group1 | NestedGroup1 | NestedGroup2 | NestedGroup3 | |||||||
Group2 | NestedGroup1 | NestedGroup2 | |||||||||
Group3 | NestedGroup1 | NestedGroup2 | NestedGroup3 | NestedGroup4 | NestedGroup5 | NestedGroup6 | NestedGroup7 | NestedGroup8 | NestedGroup9 | NestedGroup10 | |
Group4 | NestedGroup1 | NestedGroup2 | NestedGroup3 | NestedGroup4 | |||||||
Group5 | NestedGroup1 | NestedGroup2 | NestedGroup3 | NestedGroup4 | NestedGroup5 | ||||||
User2 | Group1 | NestedGroup1 | NestedGroup2 | NestedGroup3 | |||||||
Group2 | NestedGroup1 | NestedGroup2 | |||||||||
Group3 | NestedGroup1 | NestedGroup2 | NestedGroup3 | NestedGroup4 | NestedGroup5 | NestedGroup6 | NestedGroup7 | NestedGroup8 | NestedGroup9 | NestedGroup10 | |
Group4 | NestedGroup1 | NestedGroup2 | NestedGroup3 | NestedGroup4 | |||||||
Group5 | NestedGroup1 | NestedGroup2 | NestedGroup3 | NestedGroup4 | NestedGroup5 |
Thanks
Nov 09 2022 09:48 PM - edited Nov 13 2022 05:29 PM
# Filter Groups in AD based on OU
$searchBase = "OU=Test,DC=company,DC=com"
Get-ADGroup -Filter * -SearchBase $searchBase -Property * | Sort-Object Name
# Get List of Members base on Group Name
Get-ADGroupMember -Identity '<GroupName>' -Recursive | Select-Object -Property @{n="Members"; e={ $_.Name }} | Sort-Object Members
Nov 10 2022 02:03 AM
Nov 10 2022 04:13 PM
@Diego13
Hi,
Yes you can just save them in a variable like $result. Then use export to csv ps script.
$result | Export-Csv c:\temp\test.csv -NoTypeInformation
Nov 11 2022 07:39 AM
Nov 11 2022 07:45 AM
Nov 11 2022 07:49 AM
Nov 13 2022 03:59 PM - edited Nov 13 2022 04:27 PM
Hi,
The first script Get-ADGroup will get the list of group name via OU.
Then second Get-ADGroupMember -Identity '<GroupName>' the group name would be fetch from the result of the first script.
Then the third script export-csv to create the result to csv file.
Can you show me your script? or the Location of the OU for the searchbase?
Thanks.
Nov 13 2022 04:57 PM
@Diego13
Hi,
Also you could try this script. Change the <username> to your user.
(Get-ADUser '<username>' –Properties MemberOf).MemberOf
Nov 13 2022 08:23 PM
Nov 13 2022 11:24 PM
Nov 13 2022 11:30 PM
@Diego13
Hi,
Also try this one.
# Create Array
$customObject = $null
$reports = @()
# Generate CSV Filename
$file = "$($env:USERPROFILE)\Desktop\ADGroups.csv"
if (Test-Path $file) {
Remove-Item $file
}
# Filter Groups in AD based on OU
$searchBase = "OU=test,DC=domain,DC=com" # <-Change the searchbase base on your OU Location
$resultGroups = Get-ADGroup -Filter * -SearchBase $searchBase -Property * | Sort-Object Name
ForEach($resultGroup in $resultGroups){
# Get List of Members base on Group Name
Get-ADGroupMember -Identity $resultGroup.DistinguishedName -Recursive | Select-Object -Property @{n="Members"; e={ $_.Name }} | Sort-Object Members | ForEach-Object {
# Create a Custom Object
$customObject = [PSCustomObject]@{
GroupName = $resultGroup.Name
Members = $_.Members
}
$reports += $customObject
}
}
# Export result to csv
$reports | Export-Csv $file -NoTypeInformation
Write-Host "Finished..."
Nov 13 2022 11:31 PM
Nov 13 2022 11:40 PM
This one will get all the group in OU & its list of members.
# Create Array
$customObject = $null
$reports = @()
# Generate CSV Filename
$file = "$($env:USERPROFILE)\Desktop\ADGroups.csv"
if (Test-Path $file) {
Remove-Item $file
}
# Filter Groups in AD based on OU
$searchBase = "OU=test,DC=domain,DC=com" # <-Change the searchbase base on your OU Location
$resultGroups = Get-ADGroup -Filter * -SearchBase $searchBase -Property * | Sort-Object Name
ForEach($resultGroup in $resultGroups){
# Get List of Members base on Group Name
Get-ADGroupMember -Identity $resultGroup.DistinguishedName -Recursive | Select-Object -Property @{n="Members"; e={ $_.Name }} | Sort-Object Members | ForEach-Object {
# Create a Custom Object
$customObject = [PSCustomObject]@{
GroupName = $resultGroup.Name
Members = $_.Members
}
$reports += $customObject
}
}
# Export result to csv
$reports | Export-Csv $file -NoTypeInformation
Write-Host "Finished..."
Nov 13 2022 11:45 PM
@Diego13
This one can get the nested groups.
https://4sysops.com/archives/finding-nested-groups-faster-with-powershell/
Nov 14 2022 12:38 AM
Nov 14 2022 12:38 AM
Nov 14 2022 04:46 PM
@Diego13
Hi,
This one will list all the user, memberof & its OU.
Cls
$userResults = Get-ADUser -Filter 'Name -like "*"' –Properties MemberOf | Select -Property Name, MemberOf
ForEach($userResult in $userResults){
$userResult.Name
$userResult.MemberOf
}
Nov 21 2022 11:20 PM