Remove Site Columns From Libraries

RemoveSiteColumnsFromLibraries.ps1

# RemoveSiteColumnsFromLibraries: Find lists and libraries in the given site collection
# that use any of the Site Columns listed in the input XML file and delete those columns.
# Syntax example:
# .\RemoveSiteColumnsFromLibraries.ps1 http://bmphub.int/projects "D:\Scripts\SiteColumns.xml"
#=========Parameter Section=============
param(
    [string] $siteCollectionURL = $(Throw "-- You must specify the URL of a site collection as parameter 1."), #required parameter
 [string] $xmlFilePath = $(Throw "-- You must specify the pathname of an XML file as parameter 2.") #required parameter
)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"
Write-host "RemoveSiteColumnsFromLibraries: Processing Site Colleciton: " $siteCollectionURL -BackgroundColor Yellow -ForegroundColor Black
$siteScope = Start-SPAssignment
$site = get-spsite $siteCollectionURL
# get the site column XML from the input XML file
$XmlFileInput = [xml](Get-Content($xmlFilePath))
# loop over all webs in the Site Collection
foreach ($web in $site.allwebs)
{
    $webScope = Start-SPAssignment
   
    # build list of objects to save the list titles and column names to delete in just this web
    $array = @()
    # loop over lists in this subsite
    foreach ($list in $web.lists)
    {
        # only process Document Libraries: skip lists
        if ($list.BaseType -ne 'DocumentLibrary') {continue}
   
        write-host "Checking library '" $list.Title "' in subsite '" $web.Url "'" -ForegroundColor Gray 
       
        # loop over site columns listed in the input XML file
        foreach( $sitecolumn in $XmlFileInput.SiteColumns.SiteColumn)
        {
            $siteColumnToRemove = $sitecolumn.Name
   
            if ($list.Fields[$siteColumnToRemove]) # list does have this content type
            {
                if ($list.Fields[$siteColumnToRemove].Hidden) 
                {
                    write-host "    Site Column '" $siteColumnToRemove "' is HIDDEN in '" $list.Title "' in site '" $web.Url "'" -ForegroundColor Black
                }
           
                # save name of list title and site column to remove from this list in array
                $objListAndCol = New-Object System.Object
                $objListAndCol | Add-Member -type NoteProperty -name ListTitle -value $list.Title
                $objListAndCol | Add-Member -type NoteProperty -name ColName -value $siteColumnToRemove
                $array += $objListAndCol
            }
        }
    } # foreach list in web
   
    # now delete all the site columns from the lists in this SPWeb
    foreach($objListAndCol in $array)
    {
        $listTitle = $objListAndCol.ListTitle
        $colName = $objListAndCol.ColName
            
        try
        {
            $list2 = $web.lists[$listTitle]
       
            if ($list2.Fields[$colName])
            {           
                if ($list2.Fields[$colName].Hidden)
                {
                    $colToDelete = $list2.Fields[$colName] 
                    $colToDelete.AllowDeletion = $true
                    $colToDelete.Update()
                    $list3 = $web.lists[$listTitle]
                    $list3.Fields[$colName].Delete()
                    $list3.Update()
                }
                else
                {
                    $list2.Fields[$colName].Delete()
                    $list2.Update()
                }
                write-host "    Deleted column '" $colName "' from '" $listTitle "' in site '" $web.Url "'" -BackgroundColor Yellow -ForegroundColor Black
            }
        }
        Catch
        {
            write-host "Exception      :" $_.exception.message -ForegroundColor Red
            write-host "Deleting column:" $colName -ForegroundColor Red
            write-host "From List      :" $listTitle -ForegroundColor Red
            write-host "in Web         :" $web.url -ForegroundColor Red
        }
    }       
       
    Stop-SPAssignment $webScope
}
$site.dispose
Stop-SPAssignment $siteScope
Write-host "RemoveSiteColumnsFromLibraries: Done." -ForegroundColor Black

1 comment:

  1. Hi,

    Your script is the only one that really works that I have found thus far. I have been trying to verify whether a SharePoint field (column) is empty before deleting the column by using if ($list2.Fields[$colName].length -eq 0) or if ($list2.Fields[$colName]-eq $null) and I can't seem to get this to work in your script. Will it be possible for you to modify for this scenario?

    ReplyDelete