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
#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:
#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
SharePoint Online: Get Site Collection, Web ID using PowerShell SharePoint Online: Get Site Collection, Web ID using PowerShell Reviewed by Salaudeen Rajack on April 11, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.