Get Group members & Owners from Azure

 Connect-AzureAD

# Install AzureAD module if not already installed

if (-not (Get-Module -Name AzureAD -ListAvailable)) {

   Install-Module -Name AzureAD -Force -AllowClobber

}

# Import AzureAD module

Import-Module -Name AzureAD

# Connect to Azure AD

Connect-AzureAD

# Define the path to export the CSV file

$exportPath = "C:\Temp\Groups_Details.csv"

# Get all groups in Azure AD

$groups = Get-AzureADGroup -All $true

# Create an array to store group details

$groupDetails = @()

# Loop through each group

foreach ($group in $groups) {

   # Get group members

   $members = Get-AzureADGroupMember -ObjectId $group.ObjectId | Select-Object -ExpandProperty DisplayName

   # Get group owners

   $owners = Get-AzureADGroupOwner -ObjectId $group.ObjectId | Select-Object -ExpandProperty DisplayName

   # Determine group type

   if ($group.SecurityEnabled) {

       $groupType = "Security Group"

   } else {

       $groupType = "Office 365 Group"

   }

   # Get UPN details of members

   $membersUPN = $members | ForEach-Object {

       $user = Get-AzureADUser -Filter "DisplayName eq '$_'"

       if ($user) {

           $user.UserPrincipalName

       } else {

           $_  # If user not found, use display name as placeholder

       }

   }

   # Get UPN details of owners

   $ownersUPN = $owners | ForEach-Object {

       $user = Get-AzureADUser -Filter "DisplayName eq '$_'"

       if ($user) {

           $user.UserPrincipalName

       } else {

           $_  # If user not found, use display name as placeholder

       }

   }

   # Create a custom object for the group details

   $groupInfo = [PSCustomObject]@{

       "Group Name" = $group.DisplayName

       "Members" = $members -join ","

       "Members UPN" = $membersUPN -join ","

       "Owners" = $owners -join ","

       "Owners UPN" = $ownersUPN -join ","

       "Group Type" = $groupType

   }

   # Add the group details to the array

   $groupDetails += $groupInfo

}

# Export the group details to CSV

$groupDetails | Export-Csv -Path $exportPath -NoTypeInformation

# Disconnect from Azure AD

Disconnect-AzureAD

has context menu

Comments

Popular posts from this blog

How to Improve Workflow Performance in SharePoint Server 2010

PowerShell Script to Check and Generate Report on Access Rights for a Specific User:

Disable Loopbackcheck using PowerShell