export all Active Directory (AD) security groups along with their members and owners into a CSV file using PowerShell
# Import the Active Directory module
Import-Module ActiveDirectory
# Define the path for the CSV export
$csvPath = "C:\\Path\\To\\Export\\ADGroups.csv"
# Create a custom object for each group with its members and owner
$groupsInfo = Get-ADGroup -Filter * -Properties ManagedBy | ForEach-Object {
$group = $_
$members = Get-ADGroupMember -Identity $group -Recursive | Select-Object -ExpandProperty Name
$owner = (Get-ADUser -Identity $group.ManagedBy).Name
foreach ($member in $members) {
[PSCustomObject]@{
GroupName = $group.Name
MemberName = $member
OwnerName = $owner
}
}
}
# Export the information to a CSV file
$groupsInfo | Export-Csv -Path $csvP
ath -NoTypeInformation
Comments
Post a Comment