Change owner of SharePoint Online folders with PowerShell
After creating a number of folders on a common SharePoint Online site I thought it would be useful to change the owner of the folders to a generic and more ‘anonymous’ account instead of everything being owned by my personal account. I mistakenly thought that was a trivial task but I found no way of doing this in SharePoint. After some searching I came across a solution via PowerShell.
You need the PowerShell module called SharePoint Patterns and Practices (PnP) for this. If you are using a newer version of PowerShell it is easily obtained by running this command
Install-Module SharePointPnPPowerShellOnline
Once the module is installed and loaded you can connect to the SharePoint Online site in question with this command. This will work as well with Modern Authentication and Multi-Factor Authentication.
Connect-PnPOnline -Url https://company.sharepoint.com/sites/MySite -UseWebLogin
Now it’s time to pinpoint the account that you want to make owner of your folders
Get-PnPUser
This will give you a list that might look a bit like this
Select the account that you would like to make owner of the folders, e.g. ID number 12
$user = Get-PnPUser -Identity 12
Now you need the folder that you want to change the owner of
$folder = Get-PnPFolder -Url "/sites/MySite/Documents/All The Stuff"
And finally you set the owner. In this case we also change the Last Modified date just so you know how to do that as well. And then you’re done!
Set-PnPListItem -List "Documents" -Identity $folder.ListItemAllFields -Values @{Editor=""+$user.Id+"";Author=""+$user.Id+"";Modified="5/1/2020"}