This guide demonstrates how to use the Get-MgSite cmdlet in Microsoft Graph PowerShell to retrieve information about SharePoint sites. Learn how to fetch site details, filter sites, and export site data with practical examples.
The Get-MgSite cmdlet in the Microsoft Graph PowerShell module is used to retrieve SharePoint site information. This cmdlet is powerful for managing and automating various SharePoint administrative tasks. In this article, we will explore its syntax, provide usage examples, discuss common errors, and offer some tips for effective usage.
To use the Get-MgSite cmdlet,
Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "Sites.Read.All"
Get-MgSite [-SiteId <String>] [-ExpandProperty <String[]>] [-Property <String[]>] [<CommonParameters>]
Get-MgSite
This command retrieves the root site of the tenant.
Get-MgSite -SiteId "your-site-id"
Replace "your-site-id" with the actual ID of the SharePoint site you want to retrieve.
Get-MgSite -SiteId "your-site-id" -Property Id, DisplayName, WebUrl | -Select Id, DisplayName, WebUrl
This command retrieves only the Id, DisplayName, and WebUrl properties of the specified site.
Get-MgSite -SiteId "your-site-id" -ExpandProperty Owners
This command retrieves the site along with its Owners property expanded inline.
Get-MgSite -All
This command retrieves all sites within the tenant.
-Filter
parameter to narrow down results.-ConsistencyLevel
parameter to ensure that the query is handled correctly by the Graph service.-Property
parameter to optimize performance and reduce the amount of data returned.Error Message | Cause | Solution |
Error: Insufficient Permissions | The user executing the cmdlet does not have sufficient permissions to access the site information. | Ensure that the user has the necessary permissions, such as SharePoint administrator or Global administrator roles. |
Error: Invalid Site ID | The provided Site ID is incorrect or does not exist. | Verify the Site ID and ensure it is correct. You can obtain the Site ID from the SharePoint admin center or via other Graph API calls. |
Error: Property Not Found | A specified property in the -Property or -ExpandProperty parameter does not exist. |
Ensure the properties you are querying are valid and available for the SharePoint site entity. Refer to the Microsoft Graph API documentation for a list of valid properties. |
Error: Too Many Requests | The cmdlet is being executed too frequently, causing throttling by the Graph service. | Implement retry logic with exponential backoff in your script to handle throttling. |
$Sites = Get-MgSite -All $Sites | Select-Object DisplayName, WebUrl | Export-Csv -Path "C:\Path\To\SharePointSites.csv" -NoTypeInformation
domain.sharepoint.com:/sites/sitename:
Format for SimplicityGet-MgSite
like this:Get-MgSite -SiteId "domain.sharepoint.com:/sites/sitename:"
The trailing colon (:
) is required. This approach is especially useful when working with multiple sites in automation scripts.
Sites.Read.All
or Sites.ReadWrite.All
PermissionsGet-MgSite
, your Graph session must include one of the following permissions:
Sites.Read.All
— for read-only accessSites.ReadWrite.All
— for read/write accessGet-MgSite -SiteId root
syntax allows you to directly retrieve details of your tenant’s root SharePoint site — without needing to manually search for the site ID. This is especially useful for global configurations, reporting, or site-wide automation scripts.
The Get-MgSite cmdlet is a versatile tool for retrieving SharePoint site information. By understanding its syntax, usage examples, and handling possible errors, you can efficiently manage SharePoint sites in your Microsoft 365 environment. Always ensure you have the necessary permissions and utilize filtering and property selection to optimize your queries.
Note: You can retrieve SharePoint site information using the /sites
endpoint. Use /sites?search=*
to list all accessible sites or /sites/{hostname}:/site-path:
to target a specific one.
List All SharePoint Sites
# Retrieve all sites the signed-in user has access to
$response = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/sites?search=*"
if ($response.value) {
foreach ($site in $response.value) {
Write-Output "Site Details:"
Write-Output "Name : $($site.name)"
Write-Output "Web URL : $($site.webUrl)"
Write-Output "Site ID : $($site.id)"
Write-Output "`n"
}
} else {
Write-Output "No sites found or access denied."
}
💡 You can append &$select=displayName,webUrl,id
if you want to explicitly limit fields returned.
Retrieve a Specific SharePoint Site
# Replace with actual domain and site path
$hostname = "yourtenant.sharepoint.com"
$sitePath = "/sites/HRPortal"
# Properly encode the site path format
$uri = "https://graph.microsoft.com/v1.0/sites/$hostname:$sitePath"
$response = Invoke-MgGraphRequest -Method GET -Uri $uri
if ($response) {
Write-Output "Site Display Name: $($response.displayName)"
Write-Output "Web URL : $($response.webUrl)"
Write-Output "Site ID : $($response.id)"
} else {
Write-Output "Site not found or access denied."
}
⚠️ The colon (:) after the site path is mandatory in Graph API for SharePoint.
Required Permissions
You must have one of the following delegated or app permissions:
Sites.Read.All
Sites.ReadWrite.All
Graph API Documentation
© m365corner.com. All Rights Reserved. Design by HTML Codex