0

I have a script that is working to download files from an online SharePoint Document Library to a network drive. Now I just need to be able to delete the files that have already been downloaded from the SharePoint document library. I know I need each files site url but am unsure of how to collect it and then use remove-pnpfile. Here is my script:

#################### Parameters ###########################################
$webUrl = "https://***.sharepoint.com/teams/CPRPlusPST"
$listUrl = "Documents";
$destination = "\\cpr-appsprod\cprsql\Incoming"
$Username = "***"
$Password = "***"


#region Credentials 
[securestring]$SecurePass = ConvertTo-SecureString $Password -AsPlainText -Force
[System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential ($Username,$SecurePass)
#endregion Credentials

Connect-PnPOnline -Url $webUrl -Credentials $PSCredentials
$web = Get-PnPWeb
$list = Get-PNPList -Identity $listUrl

function ProcessFolder ($folderUrl,$destinationFolder) {

  $folder = Get-PnPFolder -RelativeUrl $folderUrl
  $tempfiles = Get-PnPProperty -ClientObject $folder -Property Files

  if (!(Test-Path -Path $destinationfolder)) {
    $dest = New-Item $destinationfolder -Type directory
  }

  $total = $folder.Files.Count
  for ($i = 0; $i -lt $total; $i++) {
    $file = $folder.Files[$i]

    Get-PnPFile -ServerRelativeUrl $file.ServerRelativeUrl -Path $destinationfolder -FileName $file.Name -AsFile -Force
    Write-Host -f Green "Downloaded File:" $file.Name

    ***Remove-PnPFile  -Force
    Write-Host -f Green "Deleted File from SharePoint:" $file.Name***
  }
}

function ProcessSubFolders ($folders,$currentPath) {
  foreach ($folder in $folders) {
    $file = $folder.Files
    $tempurls = Get-PnPProperty -ClientObject $folder -Property ServerRelativeUrl
    #'Signed tickets' folder only
    if ($folder.Name -eq "Signed tickets") {
      $targetFolder = $currentPath + "\";
      ProcessFolder $folder.ServerRelativeUrl.Substring($web.ServerRelativeUrl.Length) $targetFolder
      $tempfolders = Get-PnPProperty -ClientObject $folder -Property Folders
      ProcessSubFolders $tempfolders $targetFolder
    }
  }
}

#Download root files
#ProcessFolder $listUrl $destination + "\" 
#Download files in folders
$tempfolders = Get-PnPProperty -ClientObject $list.RootFolder -Property Folders
ProcessSubFolders $tempfolders $destination + "\"
0
Connect-PnPOnline -Url https://<Domain-Name>.sharepoint.com/sites/adidas *#I’m Prompted for Credentials*

$items = Get-PnPFolderItem -FolderSiteRelativeUrl "test/folder/folder1" -ItemType File
foreach ($Item in $Items)
{
   $Name = $item.Name
   Remove-PNPFile -ServerRelativeUrl "/sites/adidas/test/folder/folder1/$Name" -Force *#File’s are deleted permanently and won’t be moved to recycle bin.*
}

Hope this helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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