Export Microsoft 365 Security Group Members to CSV with PowerShell
Question details
Export Microsoft 365 security groups and their user memberships to a CSV file and address any PowerShell script execution policy error.

- 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.
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.
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.
Open PowerShell and run: Install-Module Microsoft.Graph -Scope CurrentUser. Approve the installation prompts if your organization permits it.
Run: Connect-MgGraph -Scopes "GroupMember.Read.All","User.Read.All". Sign in to the target tenant and provide or request administrator consent if prompted.
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
Use File > Open in Excel or WPS Spreadsheets to open SecurityGroupMembers.csv and review the group and user columns.

Resolve a PowerShell script execution policy error
Use this when PowerShell blocks a trusted script and your organization's policy permits a temporary change.
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.

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.




