SharePoint Online: Get Site Collection ID (or Web ID) using PowerShell

Requirement: Get the ID of a SharePoint Online site collection or subsite using PowerShell.

How to get the SharePoint Online Site ID?

If you are a SharePoint administrator, then there is a good chance you will need the IDs of your SharePoint Online site collection or subsite at times to troubleshoot issues with your site or provide it to a third-party developer. Site IDs are easy to find, and in this blog post, we will show you how to get the Site ID for your SharePoint Online site.

Here is how to find the ID of a SharePoint Online site collection or subsite with REST endpoints:

  1. To Get Site Collection ID, hit this URL in the browser: https://<tenant>.sharepoint.com/sites/<site-url>/_api/site/id
  2. To get the subsite ID (or web ID) use: https://<tenant>.sharepoint.com/<site-url>/_api/web/id
    how to get sharepoint online site id

This gets you the ID of the site, which is a GUID. The other way to get the site ID is to use the PowerShell script!

SharePoint Online: PowerShell to Get Site Collection ID

In SharePoint Online, site IDs are generated automatically by the system, and are generally not something that users need to be concerned with. However, there may be times when you need to find the site ID for a particular site. To get the site collection GUID in SharePoint Online, use this PowerShell:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Parameter
$SiteURL = "https://Crescent.sharepoint.com/Sites/Marketing"

#Get Credentials to connect
$Cred= Get-Credential
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Get Site and Web Objects
$Ctx.Load($Ctx.Site)
$Ctx.Load($Ctx.Web)
$Ctx.ExecuteQuery()

#sharepoint online powershell get site collection id
Write-host -f Green "Site ID:"$Ctx.Site.Id

#sharepoint online powershell get site id
Write-host -f Green "Web ID:"$Ctx.Web.Id

This will retrieve the ID of the specified site. You can also use this technique to find the IDs of other objects in SharePoint Online, such as lists and libraries.

PnP PowerShell to Get Site and Web IDs:

We can also retrieve the ID of the site collection with PnP PowerShell, as:

#Config Variable
$SiteURL = "https://Crescent.sharepoint.com/Sites/Marketing"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the site collection with ID property
$Site = Get-PnPSite -Includes ID

#Get Site Collection ID
Write-host -f Green "Site Collection ID:"$Site.Id 

Similarly, to retrieve the subsite ID in SharePoint Online using PnP PowerShell, use:

#Config Variable
$SiteURL = "https://Crescent.sharepoint.com/Sites/Marketing/US"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Subsite with ID property
$Web = Get-PnPWeb -Includes ID

#Get Site ID
Write-host -f Green "Site ID:"$Web.Id

To get the Tenant ID of the SharePoint Online sites, use: How to get the tenant ID in SharePoint Online?

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

One thought on “SharePoint Online: Get Site Collection ID (or Web ID) using PowerShell

  • Hello. I’ve used the first method. I’m not very technical. I copied your links into Chrome and replaced and with the right info but it gave an error no matter how I did it. I can’t use PowerShell or PnP powershell.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *