Thursday, June 25, 2009

PowerShell script to Remove user from SharePoint Group

Here is a small script I created today in PowerShell to delete a particular user from all sites of a particular web application's particular group.
It reads 3 inputs from user.
(1) Web Application URL (2) Group Name (e.g Viewers) (3) User Login Name (domain\userid)



#####################################################################################
# This script reads Web Application URL, Group Name and User Name to delete user from Group from all Sites of Web Application
# Limitations : Only one user id from only one Group of only one Web Application can be deleted
#####################################################################################

function global:RemoveUser()
{

#------------------------------------------------------------------------------------
# GAC
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") Out-Null
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") Out-Null
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") Out-Null
[System.Reflection.Assembly]::Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") Out-Null
#------------------------------------------------------------------------------------
# Read Web-Application URL
$WebApplicationUrl = "";
while ($WebApplicationUrl -eq "" -or $WebApplicationUrl -eq " " -or $WebApplicationUrl -eq $null)
{
$WebApplicationUrl = Read-Host "Enter Web Application Url to Delete User form "
if($WebApplicationUrl -eq "Q" -or $WebApplicationUrl -eq "q")
{
return;
}
#Check Required Vars
if($WebApplicationUrl -eq "" -or $WebApplicationUrl -eq $null )
{
Write-Host -ForegroundColor "Red" "`n Web Application Url to Delete User form is required. `n"
}
}
#------------------------------------------------------------------------------------
# Read Group Name
$SiteGroupName = "";
while ($SiteGroupName -eq "" -or $SiteGroupName -eq " " -or $SiteGroupName -eq $null)
{
$SiteGroupName = Read-Host "Enter Group Name to delete user from "
if($SiteGroupName -eq "Q" -or $SiteGroupName -eq "q")
{
return;
}
#Check Required Vars
if($SiteGroupName -eq "" -or $SiteGroupName -eq $null )
{
Write-Host -ForegroundColor "Red" "`n Group Name to delete user from is required. `n"
}
}
#------------------------------------------------------------------------------------
# Read User Name
$UserName = "";
while ($UserName -eq "" -or $UserName -eq " " -or $UserName -eq $null)
{
$UserName = Read-Host "Enter User Login Name to delete (e.g. Domain\UserId, System\User1) "
if($UserName -eq "Q" -or $UserName -eq "q")
{
return;
}
#Check Required Vars
if($UserName -eq "" -or $UserName -eq $null )
{
Write-Host -ForegroundColor "Red" "`n User Id is required. `n"
}
}
#------------------------------------------------------------------------------------
# Chcek and delete user if exists in Group
$Thesite = new-object Microsoft.SharePoint.SPSite($WebApplicationUrl)
$oApp = $Thesite.WebApplication
foreach ($Sites in $oApp.Sites)
{
$mySubweb = $Sites.Rootweb
Write-Host "Site URL = " $mySubweb.Url
#Write-Host "mySubweb.Title = " $mySubweb.Title

foreach ($SiteGroups in $mySubweb.SiteGroups)
{
#Write-Host "Site Group is :" + $SiteGroups.Name
if($SiteGroups.Name.ToUpper() -eq $SiteGroupName.ToUpper())
{
$oSiteGroup = $mySubweb.SiteGroups[$SiteGroupName];

#Write-Host "Site Group is :" $oSiteGroup.Name
$oUsers = $oSiteGroup.Users

foreach ($oUser in $oUsers)
{
#Write-Host "oUser.LoginName = " $oUser.LoginName
if($oUser.LoginName.ToUpper() -eq $UserName.ToUpper())
{
Write-Host "----------------------------------------------------------------------------------------"
Write-Host -ForegroundColor "black" -BackgroundColor "white" "User Found"
Write-Host "Site URL = " $mySubweb.Url
Write-Host "Site Group is :" $oSiteGroup.Name
Write-Host "oUser.Name = " $oUser.Name
$oSiteGroup.RemoveUser($oUser)
Write-Host -ForegroundColor "black" -BackgroundColor "white" "User " $oUser " deleted successfully from Group " $oSiteGroup.Name " in Site " $mySubweb.Url
Write-Host "----------------------------------------------------------------------------------------"
}
}
}
}
}
}

Thanks for visiting.