Thursday, December 15, 2016

O365 Powershell script to Delete user from given site

#Add SharePoint PowerShell SnapIn if not already added
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}


#Remove site users retireved from AD in notepad file, create RemovedUsers.txt to write the users

$url = "http://teamsite/sites/Test"

$CurrentDirectory =  Split-Path -parent $MyInvocation.MyCommand.Definition
$users= Get-content "$CurrentDirectory\users.txt"


foreach($user in $users)
{

Write-Host $user


Remove-SPUser -Identity $user -Web $Web -Confirm:$False


Add-Content RemovedUsers.txt $user

}

write-Host "Finished"

No comments:

Post a Comment