Skip to content
Rishan Solutions
Rishan Solutions
  • PowerApps
  • SharePoint online
    • Uncategorized
    • Uncategorized
  • PowerAutomate
Rishan Solutions
Latest Posts
  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025 June 24, 2025
  • Recursive Queries in T-SQL May 7, 2025
  • Generating Test Data with CROSS JOIN May 7, 2025
  • Working with Hierarchical Data May 7, 2025
  • Using TRY_CAST vs CAST May 7, 2025
  • Dynamic SQL Execution with sp_executesql May 7, 2025

Restricting Access to a SharePoint Site using PnP PowerShell

Posted on March 18, 2025March 18, 2025 by Rishan Solutions

Loading

In SharePoint Online, restricting access to a site ensures that only authorized users can view or modify its content. PnP PowerShell provides efficient commands to manage site permissions and restrict access.

This guide covers:
✔️ Removing users from a SharePoint site
✔️ Breaking permission inheritance
✔️ Restricting external sharing
✔️ Blocking site access


Prerequisites

Before proceeding, ensure:
PnP PowerShell is installed
You have SharePoint Admin or Global Admin rights
You have the site URL you want to restrict


Step 1: Install and Import PnP PowerShell

If you haven’t installed PnP PowerShell, run:

Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force

Then, import the module:

Import-Module PnP.PowerShell

PnP PowerShell is ready!


Step 2: Connect to SharePoint Online

Use the following command to connect to your SharePoint Online site:

Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive

🔹 Replace "yourtenant" with your SharePoint tenant name
🔹 Replace "yoursite" with your actual site name

Connected successfully!


Step 3: Remove Users from the SharePoint Site

To remove a specific user from a site:

# Define variables
$userEmail = "user@domain.com"

# Remove user from the site
Remove-PnPUser -LoginName $userEmail

🔹 If you want to remove multiple users, create a list:

$users = @("user1@domain.com", "user2@domain.com")
foreach ($user in $users) {
Remove-PnPUser -LoginName $user
}

Users removed successfully!


Step 4: Restrict Access by Breaking Permission Inheritance

By default, SharePoint sites inherit permissions from the tenant. To restrict access, break inheritance:

# Break inheritance on the site
Set-PnPAcl -List "Documents" -ClearExistingPermissions

🔹 This removes all existing permissions, so only admins can reassign access.

Permissions inheritance removed!


Step 5: Restrict External Sharing on the Site

To disable external sharing, use:

powershellCopyEditSet-PnPTenantSite -Url "https://yourtenant.sharepoint.com/sites/yoursite" -SharingCapability Disabled

🔹 Other options:

  • ExistingExternalUserSharingOnly → Only previously invited external users
  • ExternalUserSharingOnly → Any external users with an invitation
  • ExternalUserAndGuestSharing → Anyone with a sharing link

External sharing restricted!


Step 6: Remove SharePoint Group Access

To remove a SharePoint group from the site:

# Define group name
$groupName = "Visitors"

# Remove group
Remove-PnPGroup -Identity $groupName

Group access removed!


Step 7: Block Site Access for All Users (Except Admins)

If you want to restrict access completely, remove all users except admins:

$users = Get-PnPUser
foreach ($user in $users) {
if ($user.LoginName -notlike "*@yourtenant.onmicrosoft.com") {
Remove-PnPUser -LoginName $user.LoginName
}
}

All non-admin users removed from the site!


Step 8: Verify Site Access Restrictions

To check who has access to the site:

Get-PnPUser

Site access is now restricted!


Common Errors & Solutions

ErrorCauseSolution
Access DeniedInsufficient permissionsEnsure you are a SharePoint Admin
User not foundIncorrect email or user doesn’t existVerify the email address
Cannot remove userUser is a site ownerAssign a different owner before removal
Permission changes not applyingCached permissionsRun Clear-PnPRecycleBinItem
Posted Under PNP PowerShellexternal sharing Permissions PNP PowerShell Restrict Access Security SharePoint online site permissions

Post navigation

Granting External Users Access to a SharePoint Site using PnP PowerShell
Creating and Managing Permission Levels using PnP PowerShell

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025
  • Recursive Queries in T-SQL
  • Generating Test Data with CROSS JOIN
  • Working with Hierarchical Data
  • Using TRY_CAST vs CAST

Recent Comments

  1. Michael Francis on Search , Filter and Lookup in power apps
  2. A WordPress Commenter on Hello world!

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • March 2024
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • January 2022

Categories

  • Active Directory
  • AI
  • AngularJS
  • Blockchain
  • Button
  • Buttons
  • Choice Column
  • Cloud
  • Cloud Computing
  • Data Science
  • Distribution List
  • DotNet
  • Dynamics365
  • Excel Desktop
  • Extended Reality (XR) – AR, VR, MR
  • Gallery
  • Icons
  • IoT
  • Java
  • Java Script
  • jQuery
  • Microsoft Teams
  • ML
  • MS Excel
  • MS Office 365
  • MS Word
  • Office 365
  • Outlook
  • PDF File
  • PNP PowerShell
  • Power BI
  • Power Pages
  • Power Platform
  • Power Virtual Agent
  • PowerApps
  • PowerAutomate
  • PowerPoint Desktop
  • PVA
  • Python
  • Quantum Computing
  • Radio button
  • ReactJS
  • Security Groups
  • SharePoint Document library
  • SharePoint online
  • SharePoint onpremise
  • SQL
  • SQL Server
  • Template
  • Uncategorized
  • Variable
  • Visio
  • Visual Studio code
  • Windows
© Rishan Solutions 2025 | Designed by PixaHive.com.
  • Rishan Solutions