Forum Discussion
Powershell script to delete all versions history in Sharepoint Site
Hi LeonPavesic,
I was able to add the modules and run the Connect SPO command, but when i try to run the script you provided i get the following errors.
PS C:\Windows\system32> # Import the SharePoint Client Library
Import-Module SharePointPnPPowerShellOnline
# Connect to the SharePoint site
$siteUrl = "https://essensysuk.sharepoint.com/sites/Rohantest"
Connect-SPOService -Url $siteUrl
# Get the root folder of the SharePoint site
$rootFolder = Get-SPOFolder -Url $siteUrl
# Get all the files in the root folder
$files = $rootFolder.Files
# Iterate through each file and delete all versions but the latest 10
foreach ($file in $files) {
$versionHistory = $file.VersionHistory
# Sort the versions by creation date, descending
$versions = $versionHistory.OrderByDescending({ $_.CreationTime })
# Keep the latest 10 versions and delete the rest
$versionsToDelete = $versions | Select-Object -Skip 10
foreach ($versionToDelete in $versionsToDelete) {
$versionToDelete.Delete()
}
# Execute the changes
$ctx.ExecuteQuery()
}
# Disconnect from the
WARNING: The names of some imported commands from the module 'SharePointPnPPowerShellOnline' include unapproved verbs that mi
ght make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Ver
bose parameter. For a list of approved verbs, type Get-Verb.
Connect-SPOService : No valid OAuth 2.0 authentication session exists
At line:6 char:1
+ Connect-SPOService -Url $siteUrl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Connect-SPOService], AuthenticationException
+ FullyQualifiedErrorId : Microsoft.Online.SharePoint.PowerShell.AuthenticationException,Microsoft.Online.SharePoint.Po
werShell.ConnectSPOService
Get-SPOFolder : The term 'Get-SPOFolder' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:9 char:15
+ $rootFolder = Get-SPOFolder -Url $siteUrl
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-SPOFolder:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be
explicitly requested.
At line:15 char:10
+ foreach ($file in $files) {
+ ~~~~~
+ CategoryInfo : OperationStopped: (:) [], CollectionNotInitializedException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException
Hi rohankapz,
I'm glad you were able to install the modules and connect to your SharePoint site.
However, I'm sorry to hear that you're getting errors when trying to run the script I provided.
Here are some troubleshooting tips:
Make sure you're using the latest version of the SharePoint PnP PowerShell Online module. You can check this by running the following command:
Get-Module SharePointPnPPowerShellOnline
The version number should be at least 1.24.0. If it's not, you can update the module using the following command:
Update-Module SharePointPnPPowerShellOnline -Scope CurrentUser
Make sure you're using the correct URI syntax for your SharePoint site. The error message you're getting indicates that the URI is invalid. Try using the following format:
https://<your-sharepoint-tenant-name>.sharepoint.com/<your-site-name>
Make sure the Get-SPOFolder cmdlet is available. This cmdlet is not included in the PnP.PowerShell module by default. To install it, run the following command:
Install-Module Microsoft.Online.SharePoint.PowerShell -Scope CurrentUser
The warning message you are seeing is because the SharePoint PnP PowerShell Online module includes some commands with unapproved verbs. To find the commands with unapproved verbs, run the following command:
Import-Module SharePointPnPPowerShellOnline -Verbose
Then try to use the script one more time.
# Import the SharePoint Client Library
Import-Module SharePointPnPPowerShellOnline
# Connect to the SharePoint site
$siteUrl = "https://essensysuk.sharepoint.com/sites/Rohantest"
Connect-SPOService -Url $siteUrl
# Get the root folder of the SharePoint site
$rootFolder = Get-SPOFolder -Url $siteUrl
# Get all the files in the root folder
$files = $rootFolder.Files
# Iterate through each file and delete all versions but the latest 10
foreach ($file in $files) {
$versionHistory = $file.VersionHistory
# Sort the versions by creation date, descending
$versions = $versionHistory.OrderByDescending({ $_.CreationTime })
# Keep the latest 10 versions and delete the rest
$versionsToDelete = $versions | Select-Object -Skip 10
foreach ($versionToDelete in $versionsToDelete) {
$versionToDelete.Delete()
}
# Execute the changes
$ctx.ExecuteQuery()
}
# Disconnect from the SharePoint site
Disconnect-SPOService
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 (LinkedIn)
- rohankapzSep 29, 2023Copper ContributorHi LeonPavesic
I ran the following commands you gave and I received these errors even though I am connected to my SharePoint admin centre through the Connect SPO command. If you don't mind could we jump on a quick voice call so we can go through this? If not please can you provide further troubleshooting steps. Thanks for your help so far.
PS C:\Windows\system32> Connect-SPOService
cmdlet Connect-SPOService at command pipeline position 1
Supply values for the following parameters:
Url: https://essensysuk-admin.sharepoint.com/
PS C:\Windows\system32>
# Connect to the SharePoint site
$siteUrl = "https://essensysuk.sharepoint.com/Rohantest"
Connect-SPOService -Url $siteUrl
# Get the root folder of the SharePoint site
$rootFolder = Get-SPOFolder -Url $siteUrl
# Get all the files in the root folder
$files = $rootFolder.Files
# Iterate through each file and delete all versions but the latest 10
foreach ($file in $files) {
$versionHistory = $file.VersionHistory
# Sort the versions by creation date, descending
$versions = $versionHistory.OrderByDescending({ $_.CreationTime })
# Keep the latest 10 versions and delete the rest
$versionsToDelete = $versions | Select-Object -Skip 10
foreach ($versionToDelete in $versionsToDelete) {
$versionToDelete.Delete()
}
# Execute the changes
$ctx.ExecuteQuery()
}
# Disconnect from the SharePoint site
Disconnect-SPOService
Connect-SPOService : No valid OAuth 2.0 authentication session exists
At line:5 char:1
+ Connect-SPOService -Url $siteUrl
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Connect-SPOService], AuthenticationException
+ FullyQualifiedErrorId : Microsoft.Online.SharePoint.PowerShell.AuthenticationException,Microsoft.Onlin
e.SharePoint.PowerShell.ConnectSPOService
Get-SPOFolder : The term 'Get-SPOFolder' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
At line:8 char:15
+ $rootFolder = Get-SPOFolder -Url $siteUrl
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-SPOFolder:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Disconnect-SPOService : There is no service currently connected.
At line:31 char:1
+ Disconnect-SPOService
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Disconnect-SPOService], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.Online.SharePoint.PowerShell.Discon
nectSPOService- Steven_ReidOct 27, 2023Copper ContributorI notice that the URL in the script you are trying to run is "https://essensysuk.sharepoint.com/Rohantest" and in the scriot provided by Leon is "https://essensysuk.sharepoint.com/sites/Rohantest"
What version of PowerShell are you using?
Does the dialog box appear for you to sign in?
Is the site under "https://essensysuk.sharepoint.com/sites/"?