SharePoint Online: eliminar usuarios o grupos de permisos de carpeta mediante PowerShell
Requisito: Eliminar al usuario de los permisos de carpeta en SharePoint Online.
¿Cómo eliminar a un usuario de los permisos de carpeta en SharePoint Online?
¿Quiere restringir el acceso a una carpeta en SharePoint Online? Esta publicación de blog le mostrará cómo eliminar un usuario o grupo de los permisos de carpeta en SharePoint Online. También veremos PowerShell para revocar rápidamente el acceso de un usuario específico a una carpeta.
Para eliminar un usuario o grupo de los permisos de la carpeta SharePoint Online, siga estos pasos:
- Navegue a su lista o biblioteca de SharePoint Online donde se encuentra la carpeta de destino.
- Haga clic en "Detalles" en el menú contextual de la carpeta específica >> En el panel Detalles, haga clic en "Administrar acceso" y luego en los enlaces "Avanzado". Esto lo llevará a la página "Permisos avanzados".
Desde la cinta, haga clic en el botón "Dejar de heredar permisos" y confirme el mensaje.
- Ahora obtendrá la lista de usuarios y grupos que tienen permisos en la carpeta. Cuando se rompe el permiso, SharePoint copia los permisos de su padre (¡Lista/biblioteca en nuestro caso!)
-
Seleccione los usuarios y grupos a los que desea eliminar el permiso de la carpeta y confirme el mensaje.
- Eso es todo. Hemos eliminado a los usuarios de los permisos de carpeta.
Eliminar usuario de los permisos de la carpeta usando PowerShell
Aquí está mi PowerShell para eliminar el permiso de usuario de una carpeta en SharePoint Online:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Function Remove-SPOUserPermissionsFromList()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $FolderURL,
[Parameter(Mandatory=$true)] [string] $UserAccount
)
Try {
#Get credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
$Web = $Ctx.web
#Get the Folder
$Folder = $Web.GetFolderByServerRelativeUrl($FolderURL)
$Ctx.Load($Folder)
$Ctx.ExecuteQuery()
#Break Permission inheritence - Keep all existing list permissions & Don't keep Item level permissions
$Folder.ListItemAllFields.BreakRoleInheritance($True,$False)
$Ctx.ExecuteQuery()
Write-host -f Yellow "Folder's Permission inheritance broken..."
#Get the SharePoint User object from the site
$User = $Web.EnsureUser($UserAccount)
$Ctx.load($User)
#Get permissions assigned to the folder
$Ctx.Load($Folder.ListItemAllFields.RoleAssignments)
$Ctx.ExecuteQuery()
#Check if the user has permission on the list
[Bool]$UserFound = $False
ForEach($RoleAssignment in $Folder.ListItemAllFields.RoleAssignments)
{
$ctx.Load($RoleAssignment.Member)
$Ctx.ExecuteQuery()
#remove user permission from folder
If($RoleAssignment.Member.LoginName -eq $User.LoginName)
{
$Folder.ListItemAllFields.RoleAssignments.GetByPrincipal($User).DeleteObject()
$Ctx.ExecuteQuery()
$UserFound = $True
Write-host "User Permissions Removed from the List Successfully!" -ForegroundColor Green
}
}
#If user doesn't exist in list permissions
If($UserFound -eq $False) { Write-host "User Not found in List Permissions!" -ForegroundColor Red}
}
Catch {
write-host -f Red "Error Removing permissions from the Folder!" $_.Exception.Message
}
}
#Config Variables
$SiteURL="https://crescent.sharepoint.com"
$FolderURL="/Project Docs/Active"
$UserAccount="Salaudeen@Crescent.com"
#Call the function to remove user permissions from a list
Remove-SPOUserPermissionsFromList -SiteURL $SiteURL -FolderURL $FolderURL -UserAccount $UserAccount
Este PowerShell elimina a un usuario de los permisos de carpeta en parámetros dados.
Eliminar permisos de grupo de carpeta mediante PowerShell
De manera similar, use este script de PowerShell para eliminar un grupo de SharePoint de los permisos de la carpeta.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Function Remove-SPOGroupPermissionsFromList()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $FolderURL,
[Parameter(Mandatory=$true)] [string] $GroupName
)
Try {
#Get credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
$Web = $Ctx.web
#Get the Folder
$Folder = $Web.GetFolderByServerRelativeUrl($FolderURL)
$Ctx.Load($Folder)
$Ctx.ExecuteQuery()
#Break Permission inheritence - Keep all existing list permissions & Don't keep Item level permissions
$Folder.ListItemAllFields.BreakRoleInheritance($True,$False)
$Ctx.ExecuteQuery()
Write-host -f Yellow "Folder's Permission inheritance broken..."
#Get the SharePoint Site Group object
$Group =$Web.SiteGroups.GetByName($GroupName)
$Ctx.load($Group)
#Get permissions assigned to the folder
$Ctx.Load($Folder.ListItemAllFields.RoleAssignments)
$Ctx.ExecuteQuery()
#Check if the Group has permission on the list
[Bool]$GroupFound = $False
ForEach($RoleAssignment in $Folder.ListItemAllFields.RoleAssignments)
{
$ctx.Load($RoleAssignment.Member)
$Ctx.ExecuteQuery()
#remove Group permission from folder
If($RoleAssignment.Member.LoginName -eq $Group.LoginName)
{
$Folder.ListItemAllFields.RoleAssignments.GetByPrincipal($Group).DeleteObject()
$Ctx.ExecuteQuery()
$GroupFound = $True
Write-host "Group Permissions Removed from the List Successfully!" -ForegroundColor Green
}
}
#If Group doesn't exist in list permissions
If($GroupFound -eq $False) { Write-host "Group Not found in List Permissions!" -ForegroundColor Red}
}
Catch {
write-host -f Red "Error Removing Group permissions from the Folder!" $_.Exception.Message
}
}
#Config Variables
$SiteURL="https://crescent.sharepoint.com"
$FolderURL="/Project Docs/Active"
$GroupName="Team Site Visitors"
#Call the function to remove Group permissions from a list
Remove-SPOGroupPermissionsFromList -SiteURL $SiteURL -FolderURL $FolderURL -GroupName $GroupName
Esto elimina el grupo de SharePoint Online de los permisos de carpeta mediante PowerShell. Aquí hay otra publicación sobre cómo otorgar permiso a la carpeta de SharePoint Online usando PowerShell: Establecer permisos de carpeta en SharePoint Online usando PowerShell
PnP PowerShell para eliminar permisos de usuario de carpeta de un archivo CSV
Tenemos un archivo CSV con la lista de URL y queremos eliminar a un usuario en particular de todas esas carpetas. El archivo CSV tiene solo una columna con el encabezado "URL" y alrededor de 100 filas.
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/legal"
$ListName="Work"
$CSVFile = "C:\Temp\Folders.csv"
$UserAccount = "i:0#.f|membership|steve@crescent.com"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get content from CSV file
Import-Csv $CSVFile | ForEach-Object {
Write-host "Processing Folder:"$_.URL
#Get the Folder from URL
$Folder = Get-PnPFolder -Url $_.URL
#Get Folder Item
$FolderItem = Get-PnPProperty -ClientObject $Folder -Property ListItemAllFields
$HasUniquePerm = Get-PnPProperty -ClientObject $FolderItem -Property HasUniqueRoleAssignments
#Break Permission Inheritance
If(!$HasUniquePerm)
{
$FolderItem.BreakRoleInheritance($True, $True)
Write-host "`tFolder's Permission Inheritance Broken!"
}
#Get the User
$User = Get-PnPUser -Identity $UserAccount -ErrorAction Stop
#Get Permissions from the Folder
$RoleAssignments = Get-PnPProperty -ClientObject $FolderItem -Property RoleAssignments
#Remove user from folder permissions
[Bool]$UserFound = $false
ForEach($RoleAssignment in $RoleAssignments)
{
$Member = Get-PnPProperty -ClientObject $RoleAssignment -Property Member
If($Member.LoginName -eq $User.LoginName)
{
$UserFound = $True
$FolderItem.RoleAssignments.GetByPrincipal($User).DeleteObject()
Invoke-PnPQuery
}
}
If($UserFound) { Write-host "`tRemoved user from Folder Permission!" }
}
}
Catch {
write-host -f Red "Error Removing user from Folder:" $_.Exception.Message
}

