This script first imports the Active Directory module, sets the output file path, and then gets all the AD groups using the Get-ADGroup
cmdlet. It then loops through each group and gets its members using the Get-ADGroupMember
cmdlet. Finally, it outputs the group and its members to the specified output file using the Add-Content
cmdlet.
Note that you may need to modify the script to fit your specific environment, such as specifying a different output file path or filtering the groups to a specific organizational unit (OU).
# Import the Active Directory module
Import-Module ActiveDirectory
# Get the current date in yyyymmdd format
$date = Get-Date -Format "yyyyMMdd"
# Get a list of all AD groups and sort them by name
$groupList = Get-ADGroup -Filter * | Sort-Object Name | Select-Object Name
# Loop through each group and get its members
foreach ($group in $groupList) {
$memberList = Get-ADGroupMember -Identity $group.Name | Select-Object SamAccountName
# Output the group and its members to the file, Change the output location as per your requirements
foreach ($member in $memberList) {
$output = """$($group.Name)"",""$($member.SamAccountName)"""
Add-Content -Path "c:\ad_script\adgroupmembership-$date.csv" -Value $output
}
}
thanks for sharing details,
i require user mail id and display name also. kindly provide.
Import-Module ActiveDirectory
$groups = Get-ADGroup -Filter *
$members = foreach ($group in $groups) {
Get-ADGroupMember -Identity $group | ForEach-Object {
$user = Get-ADUser -Identity $_.distinguishedName -Properties EmailAddress, SamAccountName
[PSCustomObject]@{
GroupName = $group.Name
MemberName = $_.Name
EmailAddress = $user.EmailAddress
SamAccountName = $user.SamAccountName
}
}
}
$members | Export-Csv -Path “C:\ADGroups.csv” -NoTypeInformation
# Import the Active Directory module
Import-Module ActiveDirectory
# Get the current date in yyyymmdd format
$date = Get-Date -Format “yyyyMMdd”
# Get a list of all AD groups and sort them by name
$groupList = Get-ADGroup -Filter * | Sort-Object Name | Select-Object Name
# Loop through each group and get its members
foreach ($group in $groupList) {
$memberList = Get-ADGroupMember -Identity $group.Name | Select-Object SamAccountName, EmailAddress, DisplayName
# Output the group and its members to the file, Change the output location as per your requirements
foreach ($member in $memberList) {
$output = “””$($group.Name)””,””$($member.SamAccountName)””,””$($member.EmailAddress)””,””$($member.DisplayName)”””
Add-Content -Path “c:\ad_script\adgroupmembership-$date.csv” -Value $output
}
}
Hello sir,
i require script for office 365 users having membership of all type groups (distribution, security, teams group) .
for single and all users.
# Install and import the AzureAD module
Install-Module -Name AzureAD -Force -AllowClobber
Import-Module AzureAD
# Connect to Office 365 (Azure AD)
Connect-AzureAD
# Specify the user’s UPN (User Principal Name)
$userUPN = “user@example.com” # Replace with the actual UPN
# Get the user object from Azure AD
$user = Get-AzureADUser -ObjectId (Get-AzureADUser -UserPrincipalName $userUPN).ObjectId -ErrorAction Stop
# Get the user’s group membership
$userGroups = Get-AzureADUserMembership -ObjectId $user.ObjectId | Get-AzureADGroup -ErrorAction Stop
# Display the group membership
Write-Host “Group Membership for $($user.UserPrincipalName):”
foreach ($group in $userGroups) {
Write-Host $group.DisplayName
}