Monday, March 2, 2015

Find Lists or Libraries Using a Content Type in SharePoint with PowerShell

The following PowerShell script will loop through all the lists in a given site and look for a particular content type.  This is useful if you ever need to report on how many lists are using a content type, or you're just curious.

This script takes 2 arguments:

    Web URL - This is a fully qualified URL to the site.
    Content Type Name - This is the content type name.

So using the script would look like the following:

    Find-Content-Types.ps1 "http://SPURL/SITE" "CONTENT TYPE NAME"


############################################################################### 
##  ADD IN SHAREPOINT SNAP IN IF NOT ALREADY LOADED ## 
############################################################################### 
 
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { 
    Add-PSSnapin "Microsoft.SharePoint.PowerShell" 
}


############################################################################### 
##  SET VARIABLES FROM ARGUMENTS ## 
############################################################################### 

$webUrl = $args[0]
$ctName = $args[1]

# Varibale to hold document count
$count = 0


############################################################################### 
##  PERFORM SEARCH ## 
############################################################################### 

# Get site instance
$site = get-spsite $webUrl

# Formatting
Write-Host ""
Write-Host "Starting search...."
Write-Host ""

# Loop through each web in site, then each list
foreach ($web in $site.AllWebs)
 {
   foreach ($lst in $web.lists)
   {
     foreach ($ctype in $lst.ContentTypes)
     {
           if ($ctype.Name -eq $ctName)
           {
        Write-Host $lst.DefaultViewUrl -ForegroundColor Yellow
        $count++
    }
     }
   }
   $web.Dispose()
 }

# Formatting
Write-Host ""
Write-Host "Found $count lists using the content type '$ctName'." -ForegroundColor Green

No comments:

Post a Comment