Forum Discussion

teja1027's avatar
teja1027
Copper Contributor
May 25, 2023

Tring to pull the users from Azure AD Groups

Hi Team,

I'm trying to pull the users list from just a few 
(Not all )Azure AD groups. Is there a way to do in bulk 

  • teja1027 Hi, you can get the result using powershell , here under the script 

     

    # Import the Active Directory module
    Import-Module ActiveDirectory

    # Specify the AD group names
    $groupNames = @("Group1", "Group2", "Group3")

    # Create an array to store all group members
    $allGroupMembers = @()

    # Iterate through each group name
    foreach ($groupName in $groupNames) {
    # Retrieve group members
    $groupMembers = Get-ADGroupMember -Identity $groupName | Select-Object Name, SamAccountName

    # Add group members to the array
    $allGroupMembers += $groupMembers
    }

    # Export the group members to a CSV file
    $csvPath = "C:\Path\to\output.csv"
    $allGroupMembers | Export-Csv -Path $csvPath -NoTypeInformation

    Write-Host "Export completed. The group members have been saved to: $csvPath"

     

    Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily.

    • teja1027's avatar
      teja1027
      Copper Contributor
      Thank you so much for the response. Can I import a text file with 200 groups inside the file and try running it? Can you help me where can I insert the file and run the script.
      • teja1027 here the script modified to read the groups from csv file 

         

        # Import the Active Directory module
        Import-Module ActiveDirectory

        # Specify the path to the CSV file containing the group names
        $csvPath = "C:\Path\to\groups.csv"

        # Read the group names from the CSV file
        $groupNames = Import-Csv -Path $csvPath | Select-Object -ExpandProperty GroupName

        # Create an array to store all group members
        $allGroupMembers = @()

        # Iterate through each group name
        foreach ($groupName in $groupNames) {
        # Retrieve group members
        $groupMembers = Get-ADGroupMember -Identity $groupName | Select-Object Name, SamAccountName

        # Add group members to the array
        $allGroupMembers += $groupMembers
        }

        # Export the group members to a CSV file
        $outputCsvPath = "C:\Path\to\output.csv"
        $allGroupMembers | Export-Csv -Path $outputCsvPath -NoTypeInformation

        Write-Host "Export completed. The group members have been saved to: $outputCsvPath"

Resources