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.

You can download the complete script from the TechNet Gallery here: http://gallery.technet.microsoft.com/sharepoint/Find-Lists-or-Libraries-fd247709

For this script I wanted to use arguments when the script is run, these are very simple and include the following:
So using the script would look like the following:

So first, we'll make sure the SharePoint snap-in is loaded:
 
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { 
    Add-PSSnapin "Microsoft.SharePoint.PowerShell" 
}

Next, we'll populate some variables from the arguments:
$webUrl = $args[0]
$ctName = $args[1]

# Varibale to hold document count
$count = 0

We also include a "count" variable that will hold a count of objects that have been updated.  Now, in a try/catch block we'll execute the main code.

# 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()
 }

Finally we write out how many lists we found.
Write-Host "Found $count lists using the content type '$ctName'." -ForegroundColor Green

Here is an overview of what's going on:
Its a fairly simple script, but can save you a lot of time if you ever need it.

Thanks to Tom Benjamin for showing me the way ;)

You can download the complete script from the TechNet Gallery here: http://gallery.technet.microsoft.com/sharepoint/Find-Lists-or-Libraries-fd247709

Enjoy!

Labels: , , , , , , ,