SharePoint Online: Get Site Collection, Web ID using PowerShell

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

How to get SharePoint Online Site ID?
We may need the IDs of SharePoint Online site collection or subsite at times and these IDs are easy to find. Here is how to find the ID of a SharePoint online site collection or subsite with REST endpoints:
  • To Get Site Collection ID, hit this URL in browser: https://<tenant>.sharepoint.com/sites/<site-url>/_api/site/id
  • 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.

SharePoint Online: PowerShell to Get Site Collection ID
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://crescenttech.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

PnP PowerShell to Get Site and Web IDs:
We can also retrieve ID of the site collection with PnP PowerShell, as:
#Config Variable
$SiteURL = "https://crescenttech.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://crescenttech.sharepoint.com/Sites/Marketing"

#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

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.