Hi @Rob Smith ,
Firstly, I want to confirm what do you want to list, all site collections and subsites in the whole SharePoint tenant or the subsites under a site collection?
If you want to create a list of all site collections and subsites in your whole SharePoint tenant, Using SharePoint Online Management Shell that contains Get-SPOSite cmdlet you could retrieve information about site collections as demonstrated below for SharePoint Online (SPO):
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"
$AdminUrl = "https://tenant-admin.sharepoint.com/"
$UserName = "username@tenant.onmicrosoft.com"
$Password = "password"
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $SecurePassword
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
function Get-SPOWebs(){
param(
$Url = $(throw "Please provide a Site Collection Url"),
$Credential = $(throw "Please provide a Credentials")
)
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = $Credential
$web = $context.Web
$context.Load($web)
$context.Load($web.Webs)
$context.ExecuteQuery()
foreach($web in $web.Webs)
{
Get-SPOWebs -Url $web.Url -Credential $Credential
$web
}
}
#Retrieve all site collection infos
Connect-SPOService -Url $AdminUrl -Credential $Credentials
$sites = Get-SPOSite
#Retrieve and print all sites
foreach ($site in $sites)
{
Write-Host 'Site collection:' $site.Url
$AllWebs = Get-SPOWebs -Url $site.Url -Credential $SPOCredentials
$AllWebs | %{ Write-Host $_.Title }
Write-Host '-----------------------------'
}
$AllWebs = Get-SPOWebs -Url 'https://tenant.sharepoint.com' -Credential $SPOCredentials
$AllWebs | %{ Write-Host $_.Title }
Note: please update the $AdminUrl, $UserName, $Password variables with your SharePoint online environment.
After Successful execution of the scripts we will get the message like below:
If you want to list all subsites under a site collection, you can refer to this article:
Get All Lists details in a site collection Using Power shell in SharePoint Online
Then, you can create site column(s) in a site collection level and all of his subsites will inherit the site column(s).
About how to bulk creation of SharePoint Site Columns via Powershell, you can refer to the scripts below:
#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"
#Parameters
$SiteURL="https://xxxx.sharepoint.com"
$ColumnName="xxxx"
$IsRequired = "TRUE"
$ColumnGroup="Custom Columns"
Try {
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get all Site columns from the site
$Fields = $Ctx.web.Fields
$Ctx.Load($Fields)
$Ctx.executeQuery()
#Check if the column name exists
$NewField = $Fields | where {$_.Title -eq $ColumnName}
if($NewField -ne $NULL)
{
Write-host "Site Column $ColumnName already exists!" -f Yellow
}
else
{
#Define XML for Field Schema
$FieldSchema = "<Field Type='Text' DisplayName='$ColumnName' Name='$ColumnName' required='$IsRequired' Group='$ColumnGroup'/>"
$NewField = $Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$Ctx.ExecuteQuery()
Write-host "Site Column Created Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Creating Site Column!" $_.Exception.Message
}
Note: please update the $SiteURL, $ColumnName, $IsRequired,$ColumnGroup variables with your SharePoint online environment.
If you are still confused about this, please feel free to let me know.
If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.