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/amaretto_sh Apr 15 '26

Thank you. A lot of you guys are talking about graph and PnP modules, I'm looking into it.

Though, about ownership, I don't get why I can add ownership easily through GUI but not with scripting. Also I can get ownership on any site, I figured it was because I'm a domain's admin, is there any exception?

"You might need to use network tools to reverse engineer some of the apis." what does that mean exactly?