Security Policy Errors

Export Microsoft 365 Security Group Members to CSV with PowerShell

Phi Hung VoPhi Hung Vo Nov 27, 2026 870 views

Question details

Export Microsoft 365 security groups and their user memberships to a CSV file and address any PowerShell script execution policy error.

Export Microsoft 365 Security Group Members to CSV with PowerShell
Product
Microsoft 365 and Microsoft Graph PowerShell
Device & OS
not provided
Scenario
An administrator wants a CSV report of security group members for review or formatting in a spreadsheet.
Observed behavior
A downloaded PowerShell script reports that script execution is disabled, preventing the export.
Before you start

Confirm that you can sign in to the Microsoft 365 tenant and obtain consent for the required Microsoft Graph permissions. Check your organization's script execution policy before changing it or running downloaded code.

Solution 1Recommended

Export security group users with Microsoft Graph PowerShell

Use Microsoft Graph PowerShell to create a CSV report without relying on the older MSOnline module.

This export lists direct user members of security-enabled groups. It does not expand nested groups or list other member types, and groups without direct user members do not appear in the CSV.

The older Connect-MsolService approach uses the retired MSOnline module. Microsoft Graph PowerShell is the preferred approach for new exports.

1
Install the Graph module

Open PowerShell and run: Install-Module Microsoft.Graph -Scope CurrentUser. Approve the installation prompts if your organization permits it.

2
Connect to Microsoft Graph

Run: Connect-MgGraph -Scopes "GroupMember.Read.All","User.Read.All". Sign in to the target tenant and provide or request administrator consent if prompted.

3
Export direct user memberships

Run the following commands in PowerShell. They create SecurityGroupMembers.csv in the current folder: $groups = Get-MgGroup -Filter "securityEnabled eq true" -All $results = foreach ($group in $groups) { Get-MgGroupMemberAsUser -GroupId $group.Id -All | ForEach-Object { [PSCustomObject]@{ GroupName = $group.DisplayName GroupId = $group.Id UserName = $_.DisplayName UserPrincipalName = $_.UserPrincipalName } } } $results | Export-Csv -Path "./SecurityGroupMembers.csv" -NoTypeInformation -Encoding UTF8

4
Open the report

Use File > Open in Excel or WPS Spreadsheets to open SecurityGroupMembers.csv and review the group and user columns.

Export security group users with Microsoft Graph PowerShell
Check permissions: Your tenant may require administrator consent for the requested Graph permissions. Ask a tenant administrator if the connection or member queries are denied.
Free Microsoft Office alternative

Review your security group CSV in WPS Spreadsheets

WPS Office is a free, lightweight Office alternative with a familiar interface and Microsoft Office format compatibility for an easy transition. Open and format the exported CSV in WPS Spreadsheets; the Microsoft 365 export and policy settings still require PowerShell and the appropriate permissions.

Open and format security group membership CSV reportsCompatible with Microsoft Excel XLS and XLSX formatsFamiliar spreadsheet interface for reviewing exported dataFree and lightweight Office alternative
microsoft office alternative - wps office

Frequently Asked Questions

Where is the exported CSV saved?

The example saves SecurityGroupMembers.csv in PowerShell's current folder. Run Get-Location to see that folder, or replace the Export-Csv path with a full destination path.

Does the export include users inside nested groups?

No. Get-MgGroupMemberAsUser retrieves direct user members. Nested group memberships require a separate transitive membership query or additional processing.

Why can't I change the execution policy?

Run Get-ExecutionPolicy -List to check the effective scopes. An organization-enforced MachinePolicy or UserPolicy can override a Process-scope setting; contact your IT administrator if it blocks the script.

Can I still use Connect-MsolService for this export?

Connect-MsolService belongs to the retired MSOnline module. Use Connect-MgGraph and Microsoft Graph group commands for a new export instead.

Why are some security groups missing from the CSV?

The example writes one row per direct user member, so groups without direct user members produce no rows. It also excludes non-user members such as nested groups and service principals.