Tuesday, May 7, 2013

Powershell script to delete content type in all site collections


Hi,

We had a requirement of deleting one of the content type in all the site collections under a Web Application.
Below is the script where inputs have been provided in csv file

//DeleteContentType.bat
 cd /d %~dp0
@echo off
powershell.exe -noexit .\PSDeleteContentType.ps1 .\DeleteContentType.csv

//DeleteContentType.csv
WebAppUrl, ContentType
https://webappurl/, content type name

//PSDeleteContentType.ps1
#///<summary>
#/* Script Name                      : PSDeleteContentType
# * Purpose                             : Used to delete the content type from the web application
#///</summary>

#-----Input parameters to the script
param($inputFile)

#------To check if Sharepoint cmdlets are registered are registered
if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null ) 
            Add-PSSnapin Microsoft.SharePoint.Powershell  
#---------------------------------------------------------------------------------------------------

#-----------------------------------------Start Logging---------------------------------------------
$filepath = $MyInvocation.MyCommand.Definition                                        
$directorypath = [System.IO.Path]::GetDirectoryName($filepath)
$LogTime = Get-Date -Format yyyy-MM-dd_h-mm
$LogFile = $directorypath + "\DeleteContentType_Log_$LogTime.txt"
Start-Transcript -Path $LogFile -Force
#---------------------------------------------------------------------------------------------------

#---------------------------------------------------------------------------------------------------
# Function to delete the content type
function DeleteContentType($webAppUrl, $contentType)
{
            $wa = Get-SPWebApplication $webAppUrl

            if($wa -ne $null)
            {
                        $allSites = $wa | Get-SPSite -Limit all

                        foreach($spsite in $allSites)
                        {
                                    $allWebs = $spsite.allwebs
                                    foreach ($spweb in $allWebs) 
                                    { 
                                                try
                                                {
                                                            Write-Host 'Checking '
                                                            write-host $spweb.url
                                                            Write-Host '...'
                                                            $ct = $spweb.ContentTypes[$contentType]
                                                            if ($ct) 
                                                            {
                                                                        $ctusage = [Microsoft.SharePoint.SPContentTypeUsage]::GetUsages($ct)
                                                                        foreach ($ctuse in $ctusage) 
                                                                        {
                                                                                    $list = $spweb.GetList($ctuse.Url)
                                                                                    $contentTypeCollection = $list.ContentTypes;
                                                                                    $contentTypeCollection.Delete($contentTypeCollection[$contentType].Id);
                                                                                    Write-host "Deleted $contentType content type from $ctuse.Url..."
                                                                        }
                                                                        $ct.Delete()
                                                                        Write-host "Deleted $contentType from site..."
                                                            } 
                                                            else 
                                                            { 
                                                                        Write-host "$contentType not found..." 
                                                            }

                                                            $spweb.Dispose()                                           
                                                }
                                                catch 
                                                {
                                                            write-host $_
                                                }
                                    }
                       
                                    $spsite.Dispose()
                        }
            }
            else
            {
                        Write-Host 'Not a valid Web application url...'
            }
}
#---------------------------------------------------------------------------------------------------

#-----------------------------------------------------------------------------------
#loop through a csv file
Import-Csv $inputFile | ForEach{                              
    $webAppUrl = $_.WebAppUrl
            $contentType = $_.ContentType
                
    try
    {
                        write-host "Delete the Content Type $contentType from $webAppUrl..."
        DeleteContentType $webAppUrl $contentType
                        write-host "Completed..."
    }
    catch
    {
                        write-host "Completed with errors..."                                    
        Write-Output $_ 
    }
}
#---------------------------------------------------------------------------------------------------

#-----------------------------------------End Logging-----------------------------------------------
Stop-Transcript
#---------------------------------------------------------------------------------------------------

No comments:

Post a Comment