Site Collection: Get Web Id fails

Copper Contributor

Hi Guys,

 

I am using below code to get web Id of list of site collections using code below. The issue is i always get GUID of the first site collection howerver the other webinfo like Title, Url,etc. is correct. can anyone suggest me what is going wrong here:

foreach ($row in $data)
{

Connect-PnPOnline -Url $row.Url -Credentials $creds
$web=Get-PnPWeb

write-host "web: " + $web.Title

write-host "web: " + $web.id

}

11 Replies

Get-PnPWeb gets the web within the current site.

 

You want to use Get-PnPSubWebs to get all subwebs.

 

then select the one that you want by

 Get-PnPSubWebs | where { $_.Title -eq "sitetitle"}

Faced similar issue in the past, just initially $web with null as below and check.

 

foreach ($row in $data)
{
Connect-PnPOnline -Url $row.Url -Credentials $creds
$web=Get-PnPWeb
write-host "web: " + $web.Title
write-host "web: " + $web.id
$web=$null
}

 

 

There are 2 properties of site collection. Site Id & Web Id.

I want to find out web id of list of site collections. Above code gives me Title of web(site Collection) but fails for Web ID.

I hope it is clear.

Hi Narsima,

I have tried this already. No luck with this change.

 

Which version of PnP PowerShell are you using. I'm using the September release and Id is coming back without any problem.

I tried with September release. Still no luck. Are you providing list of site collections to the script? 

I opened 4-5 site collections in SharePoint Online Client Browser and they are showing same Web Id for all the site collections. In the screen shot below I am assuming Level 1 as Site Id and Level 2 as Web Id. Untitled.png

Are those site collections or sites?

 

I just ran this:

 

Connect-PnPOnline https://mytenant.sharepoint.com -Credentials $cred

Get-PnPWeb
 
Connect-PnPOnline https://mytenant.sharepoint.com/sites/site2 -Credentials $cred

Get-PnPWeb

Title                     ServerRelativeUrl Id                                  
-----                     ----------------- --                                  
My Team Site /                 e19d8f5e-d966-44f7-829b-c9cd39a2c462
Site 2                   /sites/site2    0b3bc433-e3f1-4415-9154-97b624a59189

These are site collections. Itried this:

Connect-PnPOnline https://mytenant.sharepoint.com/Sites/Site1 -Credentials $cred

Get-PnPWeb
 
Connect-PnPOnline https://mytenant.sharepoint.com/sites/site2 -Credentials $cred
Get-PnPWeb

and I get :

Title                  Id                                  
-----                  --------------------- --                                  
Site 1                 e19d8f5e-d966-44f7-829b-c9cd39a2c462
Site 2                 e19d8f5e-d966-44f7-829b-c9cd39a2c462

 


@Pieter Veenstra wrote:

Are those site collections or sites?

 

I just ran this:

 

Connect-PnPOnline https://mytenant.sharepoint.com -Credentials $cred

Get-PnPWeb
 
Connect-PnPOnline https://mytenant.sharepoint.com/sites/site2 -Credentials $cred

Get-PnPWeb

Title                     ServerRelativeUrl Id                                  
-----                     ----------------- --                                  
My Team Site /                 e19d8f5e-d966-44f7-829b-c9cd39a2c462
Site 2                   /sites/site2    0b3bc433-e3f1-4415-9154-97b624a59189

There are multiple site collections whhich has same Web Id. However Site Id is different for all the site collections. Also Sub Site's has unique ID's. This looks weird to me.

Do all you site collection have this problem?

 

the script below should list all your rootwebs

 

$cred = Get-Credential -Message "Password" -UserName admin@mytenant.onmicrosoft.com
Connect-PnPOnline https://mytenant.sharepoint.com -Credentials $cred

$sites = Get-PnPTenantSite 

foreach ($site in  $sites) { 
   Connect-PnPOnline $site.Url -Credentials $cred

   $web = Get-PnPWeb 
   $web
}

I can confirm that there are bunch of site collections which has same Web Id. 2 things i want to mention here: 1) we have created these(bulk) site collections using PowerShell

2) We are migrating data from SharePoint 2007 to these site collections.