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.

3 comments:

  1. wow buddy, great functionlity provided by Powershell script.

    truly speaking, till now I don't know that we can achieve these many useful functionlity from powershell script.

    good work.

    can you also please let me know how we can run this powershell script and Is there any editor available for writing script?

    Thanks,
    Sanket

    ReplyDelete
  2. Sanket, Thanks for your comments and interest. One of the best powershell editor is :
    “PowerGUI” : Editor for PowerShell scripting :
    http://dmitrysotnikov.wordpress.com/2007/10/08/notepad-for-powershell-powergui-1011-is-out/

    To run this script, first you have to install Windows Powershell to your server.
    Save the below script into file "RemoveUser.ps1" (ps1 is extension for powershell file) (u can keep any other name too)

    Then open Powershell command prompt (It is just like CMD)

    Write path of the file. This will load file in memory. (c:\data\RemoveUser.ps1)
    Then call the function in the file you want to use (in our case "RemoveUser")

    That’s it. It will asks for input and on valid input perform operation.

    ReplyDelete
  3. Thanks, but there are some other possible ways to Delete Users from SharePoint Site Collection:

    1. You can delete users using SharePoint Web Interface
    2. Delete users from SharePoint site using PowerShell (Bulk Delete also possible)
    3. Remove users from SharePoint programmatically using C#

    Find all possible ways at SharePointDiary.com: Delete Users from SharePoint Site Collection

    ReplyDelete