Bulk Sites IDs

Use PnP PowerShell to get site IDs.

#########################
# This account has to be a SharePoint Admin and also should connect to PnPOnline with this account before running the script
# Connect-PnPOnline -Url "https://x-admin.sharepoint.com" -Interactive
#########################
$SiteCollAdmin = "admin email"

# Get all site collections in the tenant
$SiteCollections = Get-PnPTenantSite

# Initialize an array to store the site information
$SiteData = @()

# Loop through each site collection and get its ID
foreach ($Site in $SiteCollections) {
        Write-Host "Processing site:" $Site.Url
    try {
        Set-PnPTenantSite -Url $Site.Url -Owners $SiteCollAdmin -ErrorAction Stop
        # Connect to each site collection
        Connect-PnPOnline -Url $Site.Url -Interactive
        
        # Get the site collection with ID property
        $SiteInfo = Get-PnPSite -Includes ID
        
        # Create a custom object with the site URL and ID
        $SiteObject = [PSCustomObject]@{
            SiteURL = $Site.Url
            SiteID  = $SiteInfo.Id
        }
        
        # Add the site object to the array
        $SiteData += $SiteObject
    }
    catch {
        Write-Host -f Red "Failed to retrieve information for site: $($Site.Url)"
    }

    Remove-PnPSiteCollectionAdmin -Owners $SiteCollAdmin
}

# Export the site data to a CSV file
$CsvFilePath = "Ids-Url.csv"
$SiteData | Export-Csv -Path $CsvFilePath -NoTypeInformation

Write-Host -f Green "Site information has been successfully exported to $CsvFilePath"