How to delete files from Powershell folder

Hi
I’m trying to delete certain files from a Sharepoint folder, however no matter what I try, I cannot get it to delete. I have an exclusions list, for the names of pictures that should not be deleted. I have attached one of many tries, but without success. Anyone know what to do?

$siteurl = “Some/Sharepoint/Folder”
$username = “AdminAccount”
$securePassword = ConvertTo-SecureString “MuchSecurePassWord” -AsPlainText -Force
$O365Credential = New-Object System.Management.Automation.PsCredential($username, $securePassword)
$count = 0
$exclusions = import-csv “Downloads\exclusions.txt”
$excounter = $exclusions.Count
write-host “Files to not delete $excounter”
Connect-PnPOnline -Url $siteurl -Credentials $O365Credential

        $items = Get-PnPFolderItem -FolderSiteRelativeUrl "/Picture Folder/Test" -ItemType File
        
        $ListItemCount = $items.Count
        Write-Host $ListItemCount
        $Found = 0
        foreach ($item in $items)
        {
            foreach ($User in $exclusions)
                {
                    #Write-Host $item.Name " " $User.User
                    if ($item.Name -eq $User.User)
                        {
                          #  Write-Host $item.Name " " $User.User
                            $Found = 1
                        }
                }
            if($Found -eq 0)
                {
                    $Name = $item.Name
                    Write-host "Delete: " $item.Name $Found
                    $item.DeleteObject()
                    
                    
                }
            $Found=0
            
        }

Hi, welcome to the forum :wave:

You haven’t provided enough information for anyone to help.

What’s the output of your Write-Host commands, is it what you would expect?

Does $items actually contain anything i.e. are there definitely files to process?

Are you getting any error messages?

Your method of looping over all the users is a bit clunky.
$exclusions.User will be an array so you can just use the -contains or -notcontains operator and do away with the flag, for example:

foreach ($item in $itemList) {
    if ($exclusions.User -notcontains $item.Name) {
        $item.DeleteObject()         
    }
}

Hi Matt

Thank you for the welcome :smiley:

Sorry for the lacking information!

The Write-host commands prints out what pictures are to be deleted, and they work properly. The print out the name.jpg for each file, that should be deleted.
When using DeleteObject() there is not error messages, but the code executes and prints out the names of the pictures correctly.

Thank for the tip! I know the code is a bit scuffed, so it is highly appreciated!

I’ve been looking for examples using the DeleteObject() method and I’m struggling to find anything relevant.

Some older documentation says that DeleteObject() fails silently if the file cannot be found.

The example code I can find for deleting items is using Remove-PnPFile. Have you tried that?

Remove-PnPFile -ServerRelativeUrl $item.ServerRelativeURL -Force 

Examples using similar code:

I’ve tried it before, but not with $item.ServerRelativeURL, so I gave it a try, but it throws

Remove-PnPFile : Parameter set cannot be resolved using the specified named parameters.
At line:31 char:25
+ ...             Remove-PnPFile -ServerRelativeUrl $item.ServerRelativeURL ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-PnPFile], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,PnP.PowerShell.Commands.Files.RemoveFile

I’ll give the two sites you sent a look aswell. :slight_smile:

Once again - Thank you for the help!