0

Over time, a lot of SPO site collections have accumulated in my tenant. Therefore, I want to use Site Collection Feature called Site Policy to automatically close and delete sites after a defined period of time.

To do that, I headed over to ContentTypeHub and created a site policy that closes a site after 6 months of its creation date, and then deletes that site after 12 months of its closure date.

When I want to apply this policy using GUI, it works well. Also, when I try to apply it using PnP PowerShell, it works. However, it is only applied at one site per-time. As mentioned, I'm dealing with a large number of sites and want to apply this policy to either all of them, or to a 10, 20, 50 of them at a time.

I used the following PnP PowerShell script to apply it to apply the policy on, at the beginning, 2 sites only:

enter image description here

2 sites, that is their URLs, are stored in a .csv file called SiteCollections.csv. This is how it looks:

enter image description here

When I run the script from above, I don't get any errors. This is my output:

enter image description here

It seems that all works fine, but when I head over to the GUI to check it, policies are not applied:

enter image description here

Is there something that I'm missing? Any help, suggestion or tip is more than welcome!

Thank you!

5
  • Does it work if you connect to a site manually and apply the policy? Nov 16 at 15:15
  • Yes, it works if I connect to a single site and run the command. When I try to do it for multiple sites (2 in this occasion), I experience this outcome. Nov 16 at 15:45
  • I wonder if the connections is the problem - try to bind the connection in the loop to a variable and use the switch -returnConnection on the cmd connect-pnp. Then on set-pnpsitepolicy, use the -connection switch and pass the connection. $CurrConnection = Connect-PnPOnline -Site $site.SiteUrl -Interactive -ReturnConnection And then Set-PnPSitePolicy -Name $PolicyName -Connection $CurrConnection Nov 16 at 15:55
  • And don't forget to close the connection when done on each site inside the loop. Nov 16 at 15:55
  • Just tried out your suggestion, unfortunately didn't work. I get the same output as last time, but when I check the sites to see if the policy is applied, it's not. Nov 17 at 14:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.