Friday, 2 November 2012

Delete All Groups

This script Deletes all SharePoint groups


param($web_app_url=$(read-host "Please provide web app url of the list to be delete (EG:'http://Wingtip'):"))

function LoadSharePointPowerShellEnviroment
{
write-host "Setting up Powershell enviroment for Sharepoint" -foregroundcolor Blue
Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
Write-host "Sharepoint PowerShell Snapin loaded." -foregroundcolor Green
}

try
{
    if($web_app_url -eq $null -or $web_app_url -eq '')
    {
        throw "You must specify your server name. Value provided was null/empty."
    }
   
    LoadSharePointPowerShellEnviroment
   
    $site = new-object Microsoft.SharePoint.SPSite($web_app_url)
    $web = $site.OpenWeb()
    $groups = $web.sitegroups
    $GroupsToDelete = @()
   
    foreach ($groupToDelete in $groups)
    {
        write-host "Group to Delete : ",$groupToDelete -foregroundcolor Magenta
        $GroupsToDelete += $groupToDelete.Name
    }
   
   
    foreach ($delgroup in $GroupsToDelete)
    {
        $web.SiteGroups.Remove($delgroup)
        write-host "Group Deleted : ",$delgroup -foregroundcolor Green
    }
   
}
catch [Exception]
{
    Write-Host -ForegroundColor Red "Error: $_"
}  
finally
{
    $web.Dispose()
    $site.Dispose()
}