Wednesday, August 14, 2024

Powershell script to read the groups using Get-MgGroup

# Step 1: Define the client credentials

$clientId= "<<client id>>"

$tenantId= "<<tenant id>>"

$clientSecret = ConvertTo-SecureString "<<client secret>>" -AsPlainText -Force


 # Step 2: Create the PSCredential object

$credential = New-Object System.Management.Automation.PSCredential($clientId, $clientSecret)


Connect-MgGraph -Credential $credential -TenantId $tenantId


# Retrieve all groups with preferred properties

$groups = Get-MgGroup -All -Property Id, DisplayName, OnPremisesSyncEnabled, mail


# Define the output file path

$excelFilePath = "C:\AzureGroupsExport\AzureADGroups.xlsx"


# Export the groups to Excel

$groups | Select-Object Id, DisplayName, OnPremisesSyncEnabled, mail | Export-Excel -Path $excelFilePath -WorksheetName "AzureADGroups" -AutoSize


# Notify the user

Write-Output "Groups have been exported to $excelFilePath"

No comments:

Post a Comment