Find Where Site Coumn Used

FindWhereSiteCoumnUsed.ps1

# FindWhereSiteCoumnUsed.ps1: Find and print out the list name and site URL of all
# the lists and libraries where each Site Column listed in the input XML file is used.
# Syntax example:
# .\FindWhereSiteCoumnUsed.ps1 http://bmphub.int/departments "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 "FindWhereSiteCoumnUsed: Processing site collection: " $siteCollectionURL -ForegroundColor Black
$siteScope = Start-SPAssignment
$site = get-spsite $siteCollectionURL
$rootweb = $site.rootweb
# get the site column XML from the input XML file
$XmlFileInput = [xml](Get-Content($xmlFilePath))
# loop over site columns listed in the input XML file
foreach( $sitecolumn in $XmlFileInput.SiteColumns.SiteColumn)
{
    $siteColumnToRemove = $sitecolumn.Name
    $sc = $rootweb.Fields[$siteColumnToRemove]
    if ($sc)
    {
        write-host "  Found Site Column '" $sc.Title "' in gallery" -ForegroundColor Gray
        foreach( $listusage in $sc.ListsFieldUsedIn() )
        {
            $listID = $listusage.ListID           
            foreach ($subweb in $site.allwebs)
            {
                foreach ($list in $subweb.lists)
                {
                    if ($list.ID -eq $listID)
                    {                    
                         write-host "    Site Column '" $sc.Title "' used in list '" $list.Title "' in site '" $subweb.Url "'" -BackgroundColor Yellow -ForegroundColor Black
                    }
                }
            }
        }
    }
}
Stop-SPAssignment $siteScope
Write-host "FindWhereSiteCoumnUsed: Done"

No comments:

Post a Comment