0

I'm downloading files from a SharePoint site to a local folder. I can connect and download files with no issue, but I go to remove them, I'm getting Error: Cannot compare "PnP.PowerShell.Commands.Model.SharePoint.RecycleResult" because it is not IComparable.

if (Test-Path -Path $DestPath$FileDownload -PathType leaf){
            Remove-PnPFile -ServerRelativeUrl $SPRelativeURL$FileDownload -Recycle -ErrorAction Ignore -Connection $connectionReport -Force
         }

The $SPRelativeURL$FileDownload is the same concatenated variable used when the file is downloaded. The process is to download the first file, confirm it was downloaded, and then remove the file from the folder on the SharePoint site. Then the process is repeated for the second file. I'm not sure what that error means. Anyone know? Full error:

Error: Cannot compare "PnP.PowerShell.Commands.Model.SharePoint.RecycleResult" because it is not IComparable.
Error: System.Management.Automation
Error:    at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

1 Answer 1

0

The error you're encountering, "Cannot compare 'PnP.PowerShell.Commands.Model.SharePoint.RecycleResult' because it is not IComparable," typically occurs when PowerShell attempts to compare objects that don't have a natural way of being compared (such as strings or integers). In your case, it seems to be related to how PowerShell handles the output of the Remove-PnPFile command.

Here's a revised approach to handle the file removal with recycling in SharePoint using PnP PowerShell:

  1. Check if the file exists before removing:

    • Before attempting to remove the file, ensure it exists in SharePoint and locally.
  2. Handle errors gracefully:

    • Use error handling to manage cases where the file might not exist or cannot be removed.

Here’s how you can structure your script:

# Define your variables
$DestPath = "C:\LocalFolder\"  # Path to local folder where files are downloaded
$SPRelativeURL = "/sites/YourSite/Library/"  # Relative URL to the SharePoint library

# Example filename (replace with actual filename variable)
$FileDownload = "ExampleFile.txt"

# Check if the file exists locally
$LocalFilePath = Join-Path -Path $DestPath -ChildPath $FileDownload
if (Test-Path -Path $LocalFilePath -PathType Leaf) {
    Write-Output "File exists locally: $LocalFilePath"
    
    # Remove the file from SharePoint (recycle)
    Remove-PnPFile -ServerRelativeUrl ($SPRelativeURL + $FileDownload) -Recycle -Connection $connectionReport -ErrorAction Ignore -Force
    if ($?) {
        Write-Output "File removed from SharePoint and recycled."
    }
    else {
        Write-Output "Error: File could not be removed from SharePoint."
    }
}
else {
    Write-Output "Error: File does not exist locally at $LocalFilePath."
}

Explanation:

  • Test-Path: Checks if the file exists locally before attempting to remove it from SharePoint.
  • Remove-PnPFile: Deletes the file from SharePoint and recycles it (-Recycle parameter). The -ErrorAction Ignore suppresses errors if the file does not exist or cannot be removed.
  • Error Handling: Uses $? to check the success of the previous command (Remove-PnPFile). $? is a built-in variable in PowerShell that indicates the success or failure of the last command executed.

Notes:

  • Ensure $SPRelativeURL and $FileDownload variables are correctly set to the SharePoint library path and the filename.
  • Adjust paths and variables according to your specific SharePoint environment and file structure.
  • Test each step carefully to ensure proper execution and error handling.

This approach should help you avoid the "Cannot compare" error and effectively manage file removal from SharePoint while recycling them. Adjustments may be needed based on specific error messages or behaviors encountered during testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.