export all AD groups and Members

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
    }
}

By amit_g

Welcome to my IT Infra Blog! My name is Amit Kumar, and I am an IT infrastructure expert with over 11 years of experience in the field. Throughout my career, I have worked with a wide variety of systems and technologies, from network infrastructure and cloud computing to hardware and software development. On this blog, I aim to share my knowledge, insights, and opinions on all things related to IT infrastructure. From industry trends and best practices to tips and tricks for managing complex systems, my goal is to provide valuable information that will help IT professionals and enthusiasts alike. Whether you are a seasoned IT veteran or just getting started in the field, I hope you will find my blog to be a valuable resource. In addition to sharing my own thoughts and ideas, I also welcome feedback, comments, and questions from my readers. I believe that a collaborative approach is the best way to advance the field of IT infrastructure and I look forward to hearing from you. Thank you for visiting my blog, and I hope you will continue to follow along as I explore the fascinating world of IT infrastructure. Sincerely, Amit Kumar

5 thoughts on “Export all Active Directory Groups and Group members in CSV file using Powershell script”
    1. 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

  1. # 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
    }
    }

    1. # 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
      }

Leave a Reply

Your email address will not be published. Required fields are marked *