r/PowerShell • u/amaretto_sh • 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!
View all comments
Show parent comment
1
u/Subject_Meal_2683 Apr 15 '26
I work with Sharepoint (actually: I develop for this fulltime in C#) and in 99% of the cases a delete on a site collection will just delete that site collection (and everything contained in it). Only when a site is linked to a team you can get problems (but in these cases you usually can't outright delete the site collection anyway).
When it comes to the SP API's: if you can delete it from the SP Admin center without any error you can also delete it using one of the API's (either Graph or the SP Api)