SharePoint Online: Get Site Collection, Web ID using PowerShell

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

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"

$SiteURL = ""

#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

#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 = ""

#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 = ""

#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.