Forum Discussion
MatthewLotts
Jan 10, 2025Copper Contributor
PowerShell 7.4 SharePoint Question
Hi everyone,
I’m seeking assistance with constructing a PowerShell script to manage document versions and space on multiple SharePoint sites. My goal is to:
- Connect to various SharePoint sites.
- Delete older copies of documents to free up space.
- Update version history settings to ensure future document control.
- Pull the list of SharePoint sites from a CSV file to handle multiple sites at once.
- Produce a log at the end of the script execution.
I’ve encountered challenges with consistent authentication methods, having tried PnP PowerShell and SharePoint REST API. I would appreciate guidance on the best approach for handling credentials and login within the script.
Any advice or examples would be greatly appreciated!
Thank you in advance for your help.
- lucheteSteel Contributor
Handling authentication with SharePoint in PowerShell scripts can be tricky sometimes, but using the SharePointPnPPowerShellOnline module works well. For consistent authentication, I recommend using stored credentials with Get-Credential or leveraging a service principal with certificate-based authentication for automation. Then to manage document versions, you can use the Set-PnPListItemVersion cmdlet to delete older versions. For accessing multiple sites, you can loop through the list of sites from your CSV and use Connect-PnPOnline for each site. Don’t forget to log actions with Start-Transcript or output to a log file for tracking.
Hope it helps!