Forum Discussion

MatthewLotts's avatar
MatthewLotts
Copper Contributor
Jan 10, 2025

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:

  1. Connect to various SharePoint sites.
  2. Delete older copies of documents to free up space.
  3. Update version history settings to ensure future document control.
  4. Pull the list of SharePoint sites from a CSV file to handle multiple sites at once.
  5. 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.

  • luchete's avatar
    luchete
    Steel 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!

Resources