r/PowerShell • • Apr 15 '26

Scripting project for SharePoint sites’ cleaning

Hello!

I’m an intern and just got the mission of cleaning useless sites from SharePoint by hand. A lot of it is repetitive and I’m pretty sure there is a way of automatising it. This project concerns < 2Go sites.

My top goals are :

  • Adding myself admin to all targeted sites in order to freely manipulate them
  • Reunite all sites created by obsolete users AND under 1Go AND unmodified (not “last visited” but “last modified”) since 2024 and delete them
  • Delete all directories unmodified since 2024 (by checking dates from all sub-directories and its content ; this one is a sensitive case because if a directory contains elements modified after 2024 but the directory in itself wasn’t modified, I really need my script to not delete it)

I’m admin in my society, with an OnMicrosoft address. I’ve already tried the first one but to no avail, and I feel like I’m not going the right direction (I get errors concerning my ID but I have all the rights and can do most of the manipulations by hand).

Is this attainable? Is it too hard for my level? Where should I dig first? What tools do I have at my disposal?

A part of me is convinced that if I can do it with GUI, it means there is a way to do it even better with a CLI, but I’m not familiar enough with PowerShell and Microsoft’s limitations to attain this goal.

Thank you all!

11 Upvotes

13 comments sorted by

View all comments

Show parent comment

1

u/Subject_Meal_2683 Apr 15 '26

I'm not saying that auto-deleting stuff without reviewing it is good (especially if you aren't familiar with Sharepoint), just telling you that deleting a Sharepoint site using one of the API's doesn't have any weird drawbacks (we have a multiple tenants with 25k+ site collections so we use the Sharepoint APIs, PnP and Graph for SP related stuff on a daily base).