Using Get-MgSite Cmdlet in Graph PowerShell

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.


Prerequisites

To use the Get-MgSite cmdlet,

  • You need one of the following Azure AD roles: Global administrator or SharePoint administrator.
  • You should install Graph PowerShell module by running the command: Install-Module Microsoft.Graph -Scope CurrentUser
  • You should connect to Graph PowerShell with the scope 'Sites.Read.All' by running the command: Connect-MgGraph -Scopes "Sites.Read.All"

Syntax

Get-MgSite [-SiteId <String>] [-ExpandProperty <String[]>] [-Property <String[]>] [<CommonParameters>]

Parameters

  • -SiteId:  Specifies the ID of the site. If not provided, it returns the root site of the tenant.
  • -ExpandProperty:   Expands related entities inline in the results.
  • -Property:   Specifies properties to include in the response.

Usage Examples


Example 1: Get the Root SharePoint Site of the Tenant


Get-MgSite

This command retrieves the root site of the tenant.


Example 2: Get a Specific Site by its ID


Get-MgSite -SiteId "your-site-id"

Replace "your-site-id" with the actual ID of the SharePoint site you want to retrieve.


Example 3: Retrieve Specific Properties of a Site


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.


Example 4: Expand Related Entities Inline


Get-MgSite -SiteId "your-site-id" -ExpandProperty Owners

This command retrieves the site along with its Owners property expanded inline.


Example 5: Get All Sites in the Tenant


Get-MgSite -All

This command retrieves all sites within the tenant.


Cmdlet Tips

  • Filtering and Pagination: Utilize filtering and pagination to handle large datasets efficiently. Use the -Filter parameter to narrow down results.
  • Consistency Level: When using queries that involve filtering, it's good practice to specify the -ConsistencyLevel parameter to ensure that the query is handled correctly by the Graph service.
  • Selective Properties: Retrieve only the necessary properties using the -Property parameter to optimize performance and reduce the amount of data returned.

Possible Errors and Solutions

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.

Frequently Asked Questions

  • What is Get-MgSite used for?
    Get-MgSite is a Microsoft Graph PowerShell cmdlet used to retrieve details about SharePoint sites, including their titles, URLs, and IDs.
  • Can I export site details to a CSV file?
    Yes, use this script to export details like site title and URL:
    $Sites = Get-MgSite -All $Sites | Select-Object DisplayName, WebUrl | Export-Csv -Path "C:\Path\To\SharePointSites.csv" -NoTypeInformation
  • What permissions are required to retrieve SharePoint sites?
    You need the Sites.Read.All or Sites.ReadWrite.All permission in Microsoft Graph PowerShell. Ensure these permissions are granted in Azure AD.
💡 Use domain.sharepoint.com:/sites/sitename: Format for Simplicity

Instead of resolving complex site IDs manually, you can use the full path format in Get-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.
🔐 You Need Sites.Read.All or Sites.ReadWrite.All Permissions

To retrieve SharePoint site details using Get-MgSite, your Graph session must include one of the following permissions:
  • Sites.Read.All — for read-only access
  • Sites.ReadWrite.All — for read/write access
Without these permissions, the cmdlet will return a 403 Forbidden or insufficient privileges error.
🌐 Easily Identify the Root SharePoint Site

The Get-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.

Conclusion

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.


If You Prefer the Graph API Way

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


Related Articles:

Using Get-MgDirectoryRole in Graph PowerShell
Using Get-MgUserLicenseDetail in Graph PowerShell
Using Find-GraphMgCommand in Graph PowerShell
Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell
Graph PowerShell Explorer Widget

20 Graph PowerShell cmdlets with easily accessible "working" examples.


Permission Required

Example:


                            


                            


                            

© m365corner.com. All Rights Reserved. Design by HTML Codex