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:

  • 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”

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:

  • First we object the site object
  • Next, we loop through all the sub-webs via the AllWebs property
  • Inside this loop is another loop that will go through all the lists in the web
  • Once we have the list, we loop through all the content types associated with the list
  • We check to see if the content type name matches what was supplied as an argument

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!

2 thoughts on “Find Lists or Libraries Using a Content Type in SharePoint with PowerShell”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>