Monday, January 16, 2012

Powershell script add content type to document library in your Sharepoint 2010 site collection

#Get site object and specify name of the library to look for in each site
$site = Get-SPSite http://yoursite.com/
$lookForList = "Document"
$ctName = "Your content type name"
#Walk through each site and change content types on the list specified
$site | Get-SPWeb -Limit all | ForEach-Object {
   
    write-host "Checking site:"$_.Title
   
    #Make sure content types are allowed on the list specified
    $docLibrary = $_.Lists[$lookForList]
   
    if ($docLibrary -ne $null)
    {
        $docLibrary.ContentTypesEnabled = $true
        $docLibrary.Update()
   
        #Add site content types to the list
        $ctToAdd = $site.RootWeb.ContentTypes[$ctName]
        $ct = $docLibrary.ContentTypes.Add($ctToAdd)
        write-host "Content type" $ct.Name "added to list" $docLibrary.Title
        $docLibrary.Update()
    }
    else
    {
        write-host "The list" $lookForList "does not exist in site" $_.Title
    }
}
#Dispose of the site object
$site.Dispose()

No comments:

Post a Comment