Monday, August 5, 2013

SharePoint PowerShell deactivate feature site collection

Please find the code to deactivate features at site collection level.

[System.Reflection.Assembly]::LoadWithPartialName("System.Xml")


function Feature_Deactivation_sitecollection($featName)
{
    try
    {
                   
        [xml]$userfile = Get-Content C:\Site.xml
        Write-Log "Feature_Deactivation_sitecollection" "Process started.."
       
        foreach($site in $userfile.Sites.Site)
        {   
            $url = $site.Url
            $spSite=Get-SPSite $url
               
                $Featuretobeactivated = Get-SPFeature -Site $spSite.Url | Where {$_.DisplayName -eq $featName}
               
               
                if($Featuretobeactivated -eq $null)
                {
                    Write-Log "Feature_Deactivation_sitecollection" "Feature not active in "$site.Url                                     
                    Write-host -foregroundcolor green "Feature not found "
                }
                else
                {
                    Disable-SPFeature -Identity $featName -Url $spSite.Url –Confirm:$false
                    Write-Log "Feature_Deactivation_sitecollection" "Feature deactivated in site collection "$site.Url
                    write-host -foregroundcolor green "Feature deactivated successfylly "$site.Url
                }
        
        }
       
        Write-Log "Feature_Deactivation_sitecollection" "Operation completed successfully.."
        write-host -foregroundcolor green "Operation completed succesfully.."
       
    }
    catch [System.Exception]
    {
        write-Host -foregroundcolor red "Exception: "$_.Exception
    }
}

function Write-Log($FunctionName,$message)
{       
    $now = Get-Date -Format "dd-MMM-yyyy HH:mm:ss"
    $now + "`t$message" >>  .\$FunctionName.txt
}

Thank you !!


No comments:

Post a Comment