Forum Discussion
Powershell script to delete all versions of files in a sharepoint site from all libraries
HiLeonPavesic ,
I am getting script error when using the script. I have all modules installed and has happened when upgrading to Windows Powershell 7.
Import-Module : The specified module 'Microsoft.SharePoint.Online.CSOM' was not loaded because no valid module file was found in any module directory.
At line:2 char:1
+ Import-Module Microsoft.SharePoint.Online.CSOM
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (Microsoft.SharePoint.Online.CSOM:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
New-Object : Cannot find type [Microsoft.SharePoint.Client.ClientContext]: verify that the assembly containing this type is loaded.
At line:6 char:8
+ $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
You cannot call a method on a null-valued expression.
At line:31 char:1
+ $ctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Hi rohankapz,
To fix the script error you are getting, you need to install the Microsoft SharePoint Online Client Library. You can do this by running the following command in PowerShell:
Install-Module -Name Microsoft.SharePointOnline.CSOM -Force -AllowClobber
Once the module is installed and imported:
Import-Module Microsoft.SharePoint.Online.CSOM
you should be able to run the script without any errors.
# Install the Microsoft SharePoint Online Client Library
Install-Module -Name Microsoft.SharePointOnline.CSOM -Force -AllowClobber
# Import the SharePoint Online Client Library
Import-Module Microsoft.SharePoint.Online.CSOM
# Connect to the SharePoint Online site
$siteUrl = "https://contoso.sharepoint.com/sites/my-site"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
# Get all the web libraries in the site
$web = $ctx.Web
$libraries = $web.Lists
# Iterate through each library and delete all versions of all files and folders
foreach ($library in $libraries) {
# Get all the files in the library
$files = $library.Files
# Iterate through each file and delete all versions
foreach ($file in $files) {
# Get the file's version history
$versionHistory = $file.VersionHistory
# Keep the latest 10 versions and delete the rest
$versionsToDelete = $versionHistory.Where({ $_.VersionLabel -eq "1.0" -or $_.VersionLabel -eq "2.0" -or $_.VersionLabel -eq "3.0" -or $_.VersionLabel -eq "4.0" -or $_.VersionLabel -eq "5.0" -or $_.VersionLabel -eq "6.0" -or $_.VersionLabel -eq "7.0" -or $_.VersionLabel -eq "8.0" -or $_.VersionLabel -eq "9.0" })
foreach ($versionToDelete in $versionsToDelete) {
$versionToDelete.Delete()
}
}
}
# Execute the script
$ctx.ExecuteQuery()
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
- rohankapzSep 21, 2023Copper ContributorI get this error when doing this as an admin on powershell
PS C:\Windows\system32> Install-Module -Name Microsoft.SharePointOnline.CSOM -Force -AllowClobber
PackageManagement\Install-Package : No match was found for the specified search criteria and module name
'Microsoft.SharePointOnline.CSOM'. Try Get-PSRepository to see all available registered module repositories.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:9711 char:34
+ ... talledPackages = PackageManagement\Install-Package @PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Ex
ception
+ FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage- LeonPavesicSep 21, 2023Silver Contributor
Hi rohankapz,
The error message you are getting means that the PowerShellGet module cannot find the Microsoft.SharePointOnline.CSOM module in any of the registered module repositories.
To fix this, you can try the following:
- Make sure that you have the latest version of PowerShell installed.
- Make sure that you have the PowerShellGet module installed. If you don't, you can install it using the following command:
Install-Module PowerShellGet
Clear the PowerShellGet module cache using the following command:
Update-Module -Name PowerShellGet -Force
Try installing the Microsoft.SharePointOnline.CSOM module again using the following command:
Install-Module -Name Microsoft.SharePointOnline.CSOM -Force -AllowClobber
If you are still getting the same error, you can try installing the Microsoft.SharePointOnline.CSOM module manually. To do this, download the module from the following website:- Microsoft.SharePointOnline.CSOM NuGet package: https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM/
Once you have downloaded the module, extract it to a folder on your computer. Then, open a PowerShell console and navigate to the folder where you extracted the module. Finally, run the following command to install the module:
Install-Module -Path .\Microsoft.SharePointOnline.CSOM
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
- rohankapzSep 21, 2023Copper ContributorI had this installed and when i upgraded to Powershell 7 it stops working, please see error attached.
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows PS C:\Windows\system32> winget search Microsoft.PowerShell Name Id Version Source --------------------------------------------------------------- PowerShell Microsoft.PowerShell 7.3.7.0 winget PowerShell Preview Microsoft.PowerShell.Preview 7.4.0.5 winget PS C:\Windows\system32> Install-Module PowerShellGet
PS C:\Windows\system32> Update-Module -Name PowerShellGet -Force
WARNING: The version '1.4.8.1' of module 'PackageManagement' is currently in use. Retry the operation after closing the applications.
PS C:\Windows\system32> Install-Module -Name Microsoft.SharePointOnline.CSOM -Force -AllowClobber
PackageManagement\Install-Package : No match was found for the specified search criteria and module name 'Microsoft.SharePointOnline.CSOM'. Try Get-PSRepository to see all available registered module repositories.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:9711 char:34
+ ... talledPackages = PackageManagement\Install-Package @PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Exception
+ FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage
PS C:\Windows\system32>