Home

SharePoint Online: PowerShell script to Remove a defined column accross the Site collection

Highlighted
Fabrice Romelard
MVP

SharePoint Online: PowerShell script to Remove a defined column accross the Site collection

Our large SharePoint Online Site collection used for Intranet Corporate Portal based on Publishing site, we have a large number of subsites into.

 

We had to remove a field place into the page library of all those subsites.

The following script was built to do that removal.

You can adapt it as you want for your own requirements.

 

[string]$username = "Admin@yourtenant.onmicrosoft.com"
[string]$PwdTXTPath = "C:\SECUREDPWD\ExportedPWD-$($username).txt"
$secureStringPwd = ConvertTo-SecureString -string (Get-Content $PwdTXTPath)
$adminCreds = New-Object System.Management.Automation.PSCredential $username, $secureStringPwd

[string]$RootSiteToCheck = "https://yourtenant.sharepoint.com/sites/YourSiteCollection"
[string]$SPListToCheck = "Pages"
[string]$SPFieldInternalNameToCheck = "PublishedDate"

function Load-DLLandAssemblies
{
	[string]$defaultDLLPath = ""
	# Load assemblies to PowerShell session 
	$defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
	[System.Reflection.Assembly]::LoadFile($defaultDLLPath)
	$defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
	[System.Reflection.Assembly]::LoadFile($defaultDLLPath)
	$defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
	[System.Reflection.Assembly]::LoadFile($defaultDLLPath)
}

function Check-And-Fix-Field-In-SPList ([Microsoft.SharePoint.Client.ClientContext]$Context, [Microsoft.SharePoint.Client.Web]$CurrentWeb)
{
	$MyCurrentListTocheck = $CurrentWeb.Lists.GetByTitle($SPListToCheck)
	$AllSPListCT = $MyCurrentListTocheck.ContentTypes
	$Context.Load($MyCurrentListTocheck)
	$Context.Load($AllSPListCT)
	$Context.ExecuteQuery()
	Write-Host "  ===>>> SubSite to check:", $CurrentWeb.Title, "- URL:", $CurrentWeb.Url -ForegroundColor Green
	Write-Host "  ===>>> List to check:", $MyCurrentListTocheck.Title -ForegroundColor Green
	foreach($MySpListCT in $AllSPListCT)
	{
		Write-Host "         -->> Content Type Name:", $MySpListCT.Name
		Write-Host "         -->> Content Type ID:", $MySpListCT.id
		$Myfields = $MySpListCT.Fields
		$Context.Load($Myfields)
		$Context.ExecuteQuery()
		$MyfieldToCheck = ($Myfields | where {$_.InternalName -eq $SPFieldInternalNameToCheck})
		if($MyfieldToCheck -ne $null)
		{
			Write-Host "             ---------------------------------------------- " -ForegroundColor Yellow
			Write-Host "             >>>> Field Name:", $MyfieldToCheck.Title -ForegroundColor Yellow
			Write-Host "             >>>> Field InternalName:", $MyfieldToCheck.InternalName, "- Field ID:", $MyfieldToCheck.id -ForegroundColor Yellow
			Write-Host "             >>>> Field Required:", $MyfieldToCheck.Required -ForegroundColor Yellow
			Write-Host "             ---------------------------------------------- " -ForegroundColor Yellow
			$MyfieldToCheck.DeleteObject();
			$MySpListCT.Update($false);
			$Context.Load($MySpListCT);
			$Context.ExecuteQuery()
			Write-Host "             >>>> Field Deleted !!!!" -ForegroundColor Red
		}
	}
}

function Get-SPOSubWebs
{
	Param( 
        [Microsoft.SharePoint.Client.ClientContext]$Context, 
        [Microsoft.SharePoint.Client.Web]$RootWeb
    ) 
	$Webs = $RootWeb.Webs
	$Context.Load($Webs)
	$Context.ExecuteQuery()
	ForEach ($sWeb in $Webs)
	{
		Write-host "   ====>> SubSite:", $sWeb.URL -ForegroundColor red
		Check-And-Fix-Field-In-SPList $Context $sWeb
		Get-SPOSubWebs -RootWeb $sWeb -Context $Context
	} 
} 

cls
Write-Host " ---------------------------------------------- "
Load-DLLandAssemblies
Write-Host " ---------------------------------------------- "
Write-host "===================================================================================================" -ForegroundColor Green
$mySitectx = New-Object Microsoft.SharePoint.Client.ClientContext($RootSiteToCheck)
$mySitectx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($adminCreds.UserName, $adminCreds.Password)
$mySitectx.RequestTimeout = 1000000 # milliseconds
$myCurrentWeb = $mySitectx.Web
$mySitectx.Load($myCurrentWeb)
$mySitectx.ExecuteQuery()
Write-Host " "
Write-Host " ---------------------------------------------------------"
Write-Host "  >>>> # Server Version:" $mySitectx.ServerVersion " # <<<<<<" -ForegroundColor Green 
Write-Host " ---------------------------------------------------------"
Write-Host " "
Write-host "==================================================================================================="
Write-host "   -->> RootSite:", $myCurrentWeb.URL -ForegroundColor green
Write-host "==================================================================================================="
Check-And-Fix-Field-In-SPList $mySitectx $myCurrentWeb
Get-SPOSubWebs $mySitectx $myCurrentWeb
Write-host "==================================================================================================="

Fabrice Romelard

 

French version:

Source used:

Related Conversations
Embedding on SharePoint
Audrie Gordon in Microsoft Stream Forum on
46 Replies
External Sharing with Stream or Video
Jennifer Seitsinger in Microsoft Stream Forum on
46 Replies
Cutomizing SharePoint Online Modern Page
Vikas Dhingra in SharePoint on
4 Replies
Adding a user or sending download link
Scott Mackay in Office 365 on
8 Replies
Excel Share document look and feel
Jerry Gonzalez in Excel on
8 Replies