Exporting SharePoint Online Site Users to CSV with PnP PowerShell

Introduction

Understanding who has access to a SharePoint Online site is one of the first steps in any governance, migration, auditing, or reporting project. Whether you are validating permissions before a migration, documenting site membership, or building administrative reports, having a complete inventory of the site’s known users is extremely valuable.

In the previous article, we created a reusable connection script using PnP PowerShell and Interactive Authentication. In this article, we build upon that foundation by extending the script to export the site’s users into a CSV file.

The solution follows the same philosophy adopted throughout this series:

  • Use native Microsoft 365 tools whenever possible.
  • Use PnP PowerShell for SharePoint administration.
  • Keep scripts modular and reusable.
  • Produce CSV files suitable for reporting tools such as Excel and Power BI.

What Does Get-PnPUser Return?

One important point to understand is what Get-PnPUser actually returns.

This cmdlet queries the SharePoint User Information List associated with the site.

Typically, this includes:

  • Site Owners
  • Site Members
  • Site Visitors
  • Individual users
  • SharePoint Groups
  • Microsoft Entra ID groups that have accessed the site
  • Users previously resolved by SharePoint

It is important to note that Get-PnPUser does not automatically expand Microsoft Entra ID groups such as “Everyone except external users” into individual users. If a Microsoft Entra ID group has been granted permissions, the cmdlet usually returns the group itself rather than every member contained within it.

This behavior makes the cmdlet ideal for understanding how SharePoint knows the principals associated with a site, but it should not be interpreted as a complete directory export.


Complete Script

# =====================================================================
# SharePoint Online - Export Site Users
# =====================================================================
Clear-Host
Write-Host ""
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host " SharePoint Online - Export Site Users" -ForegroundColor Cyan
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host ""
# Verify whether PnP PowerShell is installed
if (-not (Get-Module -ListAvailable -Name PnP.PowerShell))
{
Write-Host "PnP.PowerShell is not installed." -ForegroundColor Yellow
Write-Host "Installing PnP.PowerShell..." -ForegroundColor Yellow
Install-Module PnP.PowerShell -Scope CurrentUser -Force
}
Import-Module PnP.PowerShell
# App Registration Client ID
$ClientId = "YOUR-CLIENT-ID"
# Ask for the Site URL
$SiteUrl = Read-Host "Enter the SharePoint Site URL"
# Connect
Connect-PnPOnline `
-Url $SiteUrl `
-ClientId $ClientId `
-Interactive
# Validate connection
$Web = Get-PnPWeb
Write-Host ""
Write-Host "Connected to: $($Web.Title)" -ForegroundColor Green
Write-Host ""
# Output file
$CsvFile = "SiteUsers.csv"
# Export users
Get-PnPUser |
Select-Object `
Title,
Email,
LoginName,
PrincipalType,
IsSiteAdmin |
Export-Csv `
-Path $CsvFile `
-NoTypeInformation `
-Encoding UTF8
Write-Host ""
Write-Host "Export completed successfully." -ForegroundColor Green
Write-Host "CSV File: $CsvFile"
Write-Host ""

Script Walkthrough

Step 1 – Connect to the Site

The script first establishes a secure connection using Interactive Authentication.

Because the URL is entered at runtime, the same script can be reused for any SharePoint Online site.


Step 2 – Validate the Connection

After authentication, the script retrieves the current web:

$Web = Get-PnPWeb

This confirms that authentication succeeded before attempting to retrieve any information.


Step 3 – Retrieve Site Users

The core command is:

Get-PnPUser

This returns every principal registered in the site’s User Information List.


Step 4 – Select Relevant Properties

Rather than exporting every available property, the script keeps only the information most commonly required during administration.

PropertyDescription
TitleUser display name
EmailEmail address
LoginNameClaims login
PrincipalTypeUser, SharePoint Group or Security Group
IsSiteAdminIndicates whether the user is a Site Collection Administrator

This produces a clean CSV that is easy to consume.


Step 5 – Export the CSV

The final result is written using:

Export-Csv

The generated file uses UTF-8 encoding and can be opened directly in:

  • Microsoft Excel
  • Power BI
  • Power Query
  • Any CSV-compatible reporting tool

Example Output

TitleEmailLoginNamePrincipalTypeIsSiteAdmin
John Smithjohn@contoso.comi:0#.f|membership|john@contoso.comUserFalse
Mary Jonesmary@contoso.comi:0#.f|membership|mary@contoso.comUserFalse
Site OwnersSite OwnersSharePointGroupFalse

Common Use Cases

This script is useful for:

  • Governance reports
  • Security audits
  • Migration assessments
  • Permission validation
  • Site documentation
  • Power BI datasets
  • Inventory of SharePoint sites

Limitations

Although extremely useful, Get-PnPUser should not be confused with an export of every Microsoft Entra ID user.

The cmdlet exports the principals known by SharePoint for the current site. Microsoft Entra ID groups are generally returned as groups rather than expanded into their individual members.

If your objective is to enumerate the members of Microsoft Entra ID groups, Microsoft Graph should be used instead.


Conclusion

Exporting SharePoint site users is one of the simplest yet most valuable administrative tasks. With only a few lines of PowerShell, administrators can generate a reusable inventory suitable for governance, reporting, migration planning, and security reviews.

By keeping the authentication logic separate and reusing a common connection pattern, future scripts can focus exclusively on their administrative task while maintaining a consistent and maintainable code base.

This script will serve as the foundation for many of the upcoming SharePoint administration tools in this series.


Official References

Edvaldo Guimrães Filho Avatar

Published by