“Who can open the Finance library, and why?” In most organisations nobody can answer that without half an hour of clicking through permission pages. I ran into this again at an organisation with two offices and about a hundred people, and this time I decided to solve it properly. The result is a small set of scripts that runs SharePoint access from one matrix in a spreadsheet. I have published it as open source: sharepoint-rbac-toolkit.
Permissions drift, and nobody notices
SharePoint makes sharing easy, and that is exactly the problem. Someone shares a folder with a colleague. A site owner adds three people by hand because it was urgent. A group is renamed in Entra ID and shows up under its old name for months. Somebody leaves, and their account quietly keeps its access to a board document.
None of these is a disaster on its own. Together they mean that after a year or two the permissions no longer describe a decision anyone took. They describe the history of who asked whom for what.
Fixing it by hand does not work either. Every library has its own permission page, every person has to be looked up one by one, and when you are done there is nothing to show the person who should sign it off.
Three rules
The model I ended up with is not new. It is plain role-based access, and Microsoft recommends it: work with groups, not with individual people. What made the difference was holding on to three rules without exceptions.
- People are only ever members of role groups. A role group belongs to a job: Finance Team, Site Managers, HR Advisor. These are ordinary security groups in Entra ID.
- A matrix says which role reaches what. Roles down the side, libraries or sites across the top, a mark where a role has access.
- Nothing else gets access. No people directly on a library, no sharing links on a library, no groups that are not in the matrix.
A new colleague gets access by joining a role group. A leaver loses it by leaving the group. A change in who may see what is one mark in the matrix.
| Role | Procedures | Customers | Finance | Board |
|---|---|---|---|---|
| General Manager | x | x | x | x |
| Finance Team | x | x | x | |
| Customer Service | x | x | ||
| Auditor | Read | Read | Read |
That table is the whole access policy. A finance director can read it, change it and approve it. No permission page required.
The owner owns the matrix, not IT
This turned out to be the part that matters most. The matrix lives as a tab in an Excel workbook that the scripts generate. The other tabs show the current state straight from Entra ID: per person their job title, their roles and what those roles give them, and per group its members.
The business owner edits the matrix tab and sends the file back. IT imports it, the script lists every difference in plain terms (“Customer Service gains Business Development, HR loses Rentals”), and after the change a fresh workbook goes back as confirmation.
That turns “can you give Jane access to Finance?” from a favour into a decision. Either Jane’s role should have Finance, which is a mark in the matrix for everyone in that role, or Jane is changing role. The request “just this once, just for her” has nowhere to go, and that is the point.
What the scripts do
There are eleven PowerShell scripts and three small Python helpers, and they fall into three kinds.
Look. Take a snapshot of the groups and members in Entra ID. Compare every library and site with the matrix and list each difference. Find the files and folders that were shared with individual people. These scripts cannot change anything.
Change. Make SharePoint match the matrix. Create missing groups and fix how they are nested. Add someone to a role or take them out. Remove everything a leaver still held. All of these are a dry run unless you add -Apply.
Report. Build the Excel workbook, import the matrix the owner sent back, and produce a list of individually shared items with a Keep or Remove dropdown per row.
Two models are supported. In the first, role groups sit directly on the libraries of one site, each at its own permission level. In the second, every department site has one access group and the role groups are nested inside it, so a change in the matrix never touches SharePoint at all.
What I found when I first ran the audit
The audit is read-only, so it was the first thing I ran. A few findings, which I suspect you will recognise:
- A department site where several role groups had been added one by one over the years. Three of them had no business there according to the matrix that the owner had signed off.
- A device-management group, meant for laptop policies, that was a member of a department site. Everyone in it could edit that site.
- A disabled account that was still a member of a group.
- Ten files and folders in sensitive libraries that had been shared with individuals, 27 shares in total. Most of those people had access through their role anyway. A few did not, and for those the owner had to decide.
- Groups that appeared under two names, because SharePoint keeps the name a group had when it was first used on a site.
None of this was visible from any admin screen. All of it was visible within ten minutes of running two scripts.
Things that cost me an afternoon each
A few lessons that are now built into the toolkit.
The two PowerShell modules cannot share a session. The PnP module for SharePoint and the Microsoft Graph module ship conflicting assemblies. Load both and you get an error or, worse, an empty result without an error. Every script therefore loads exactly one of them and runs in its own process. The SharePoint scripts cannot ask Entra ID for a group’s id, so they read it from the snapshot.
SharePoint Administrator does not mean access to sites. The role lets you make yourself site administrator. Until you do, a site answers “unauthorized”.
Match on the object id, never on the name. Names in SharePoint go stale. Ids do not.
Limited Access is a symptom. When a file inside a library is shared with someone, SharePoint adds that person to the library with Limited Access. Remove that and you break the share. The scripts leave it alone and report the share itself.
A dry run has to be the default. Not a flag you remember to add, but what happens when you do nothing. Every script that can change something prints what it would do and stops.
Built with an AI assistant, and built for one
I wrote these scripts together with Claude Code, and I use them the same way: I describe the request, the assistant picks the script, does the dry run and shows me the result. I decide whether it applies.
That only works safely because of how the scripts are built. Each one explains itself in its help text. Each one says in its output whether it was a dry run. And the repository contains an instruction file with rules for the assistant: never grant a person access directly, always do a dry run first and wait, never retry a sign-in in a loop, read the actual state back before reporting that something is done.
The rules matter more than the prompts. An assistant that follows the rules and uses a poor prompt does the right thing slowly. One that has a good prompt and no rules will happily share a folder with one person because you asked nicely.
One caveat: this needs an assistant that can read the files and run commands, such as Claude Code or GitHub Copilot in agent mode. A chat window that cannot see the scripts will invent parameters.
What it does not do
It does not decide who should have access. That stays a business decision; the toolkit makes the decision visible and enforces it.
It works with security groups, not with Microsoft 365 groups or Teams membership. It manages sites and libraries; single files and folders are audited and cleaned up, not governed. And it has no unattended mode: every run is a person signing in with their own account. For permissions, I think that is right.
Try it
The repository has two example configurations and a self-test that runs without signing in anywhere. Start with the two read-only scripts on your own tenant: the snapshot and the audit. They change nothing, and I would be surprised if they show you nothing new.
If you find a case the scripts do not handle, open an issue. SharePoint tenants are all messy in their own way, and I have only seen a few of them.
Further reading: