Remove SharePoint Groups using PowerShell

Disclaimer: This post and function is 100% taken from the book by Gary Lapointe and Shannon Bray entitled “Automating Microsoft SharePoint 2010 Administration with Windows PowerShell 2.0.”

This post goes hand in hand with my two recent posts entitled “Retrieve SharePoint Groups using PowerShell” and “Create SharePoint Groups using PowerShell.”  In those posts, we used PowerShell Functions to retrieve SP Groups and to create SP Groups.  The third piece of the puzzle is Remove-SPGroup.

Again, I have taken the code from Gary and Shannon’s book – however, this function adds comment-based help as well as a confirm prompt.  I thought it would be nice to add the ability to cancel out of the operation, plus it just looks fancy – and I like fancy.

Here’s the Remove-SPGroup function:

function Remove-SPGroup {
	Use Remove-SPGroup to delete a SharePoint Group.
	This function uses the Remove() method of a SharePoint RoleAssignments property in an SPWeb to create a SharePoint Group.
	C:\PS>Remove-SPGroup -Web http://intranet -Group "Test Group"
	This example removes a group called "Test Group" in the http://intranet site.
	C:\PS>$web = Get-SPWeb http://intranet
	C:\PS>$group = Get-SPGroup -Web $web -Group "Test Group"
	C:\PS>Remove-SPGroup $web $group
	This example also removes a group called "Test Group" from the http://intranet site, but this example uses $web and $group variables.
	Name: Remove-SPGroup
	Author: Ryan Dennis
	Last Edit: July 18th 2011
	Keywords: Remove-SPGroup
#Requires -Version 2.0
$SPWeb = $Web.Read()
# Prompting code
$title = "Delete SharePoint Group"
$message = "Do you want to delete the SharePoint Group?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Deletes the SharePoint Group."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Cancels and keeps the SharePoint Group."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $Host.UI.PromptForChoice($title,$message,$options,0)
switch ($result)
	0 {"Deleting $($Group) Group."}
	1 {"Operation cancelled..."}
# End Prompting code
if ($result -eq 0){
else {return}