########################################################## # # Script-Name: Remove-SPUser.ps1 # # authors: Niklas Goude # Mattias Karlsson # # Url: http://mysharepointofview.com # http://www.PowerShell.nu # ########################################################## param ([string]$Site, [string]$SiteCollection, [string]$User, [switch]$help) function GetHelp() { $HelpText = @" DESCRIPTION: NAME: Remove-SPUser.ps1 Removes a User from the Site Collection PARAMETERS: -Site Url to the Sharepoint Site (Required) -SiteCollection Name of Site Collection (Required) -User User that you want to remove (Required) -help Prints the HelpFile (Optional) SYNTAX: ./Remove-SPUser http://wss Test Domain\User Removes the domain user Domain\User from the Test SiteCollection http://wss/Test ./Create-Sites.ps1 -help Displays the help topic for the script "@ $HelpText } function RemoveUser([string]$Site, [string]$SiteCollection, [string]$User) { # GAC [System.Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null # Connect To Sharepoint $SPSite = New-Object Microsoft.SharePoint.SPSite($Site) $OpenWeb = $SPSite.OpenWeb($SiteCollection) # Check if User Exists in Site if ($OpenWeb.SiteUsers | Where {$_.LoginName -eq $User}) { $User = $OpenWeb.SiteUsers | Where {$_.LoginName -eq $User} $OpenWeb.SiteUsers.Remove($User) Write-Host "User: $User Successfully Removed from Site: $Site/$SiteCollection" -ForegroundColor Green } else { Write-Host "User: $User Does not exist on Site: $Site/$SiteCollection" -ForegroundColor Yellow } $SPSite.Dispose() $OpenWeb.Dispose() } if($help) { GetHelp } if($Site -AND $SiteCollection -AND $User) { RemoveUser $Site $SiteCollection $User } else { GetHelp }