Deleted files after downloaded from sharepoint site

Hello !

I have a script that download files from sharepoint site to a local server and log them in a file.

I want to modify this script by delete the sharepoint files 5 second previously copied but i have this error. Thanks for help me :slight_smile:

Remove-PnPFile : A positional parameter cannot be found that accepts argument 'Documents/Extractions_Test'.
At C:\PS\CopyFromSharePointToLocal_TESTThomas_V1 - Copy.ps1:35 char:5
+     Remove-PnPFile -ServerRelativeUrl -Path $SharePointFolderPath -Fi ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-PnPFile], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,PnP.PowerShell.Commands.Files.RemoveFile
# Variable - A modifier selon le contexte
$O365ServiceAccount="xxxxxxxxx"# Your User name  
$O365ServiceAccountPwd="xxxxxxxxxx"#Your Password - You can use Get-credentials command as well  
$SharePointSiteURL="https://tractafric.sharepoint.com/Intranet/Projets" # Change this SharePoint Site URL
$Logfile="C:\pipe\files\testthomas\log\log_testthomas.txt"
$StartTime = Get-Date # Récupère la date
 
$SharedDriveFolderPath="C:\pipe\files\testthomas"  
$SharePointFolderPath="Documents/Extractions_Test"  

$env:PNPLEGACYMESSAGE='false'

# Sécurisation du mot de passe lors de la connexion  
[SecureString]$SecurePass = ConvertTo-SecureString $O365ServiceAccountPwd -AsPlainText -Force  
[System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass) 
 
# Connexion au site SharePoint Online  
Connect-PnPOnline -Url $SharePointSiteURL -Credentials $PSCredentials  

# Lecture des fichiers dans SharePoint Document Library Folder /Extractions Test Thomas B
$Files=Get-PnPFolderItem -FolderSiteRelativeUrl $SharePointFolderPath -ItemType File

# Wait for 5 seconds
Start-Sleep -Seconds 5


foreach ($File in $Files) {
    # Télécharge les fichiers to Network Shared Path et log le nom des fichiers
    "Downloading File $($File.Name)" | Add-Content -Path "C:\pipe\files\testthomas\log\log_testthomas-$($StartTime.ToString("yyyyMMdd")).txt"
    Get-PnPFile -Url $File.ServerRelativeUrl -Path $SharedDriveFolderPath -FileName $File.Name -AsFile
    
    #Delete the Sharepoint files
    Remove-PnPFile -ServerRelativeUrl -Path $SharePointFolderPath -FileName $File.Name -AsFile
} 

Assuming this is the documentation:

Remove-PnPFile | PnP PowerShell

There is no -Path or -FileName param and the path (including file) should be passed as -SiteRelativeUrl

Remove-PnPFile [-SiteRelativeUrl] <String> [-Recycle] [-Force] [-Connection <PnPConnection>]

So what can i use to deleted these files ?

Hard to assist as we cannot see the variable values, but you need to build the path to the file like the documentation shows:

Remove-PnPFile -ServerRelativeUrl /sites/project/_catalogs/themes/15/company.spcolor

The strings from the variables would need to be likely concatenated to build a full relative path to the file, something like this:

Remove-PnPFile -ServerRelativeUrl ('{0}/{1}' -f $SharePointFolderPath, $File.Name)

I tried, but i have the output below:

Remove-PnPFile : serverRelativePath
Parameter name: La valeur spécifiée n'est pas prise en charge pour le paramètre serverRelativePath.
At C:\PS\CopyFromSharePointToLocal_TESTThomas_V1 - Copy.ps1:36 char:4
+    Remove-PnPFile -ServerRelativeUrl ('{0}/{1}' -f $SharePointFolderP ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Remove-PnPFile], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,PnP.PowerShell.Commands.Files.RemoveFile

What is Get-PNPFile returning? Does it have a property ServerRelativeUrl? Can you do

 $pnpFile =  Get-PnPFile -Url $File.ServerRelativeUrl -Path $SharedDriveFolderPath -FileName $File.Name -AsFile

#Show output to see if the data looks correct for troubleshooting    
'Deleting file {0}' -f $pnpFile.ServerRelativeUrl
    #Delete the Sharepoint files
if ($pnpFile) {
    Remove-PnPFile -ServerRelativeUrl $pnpFile.ServerRelativeUrl
}

The goal is using the variables or the output from Get-PnPFile to provide your Remove-PnPFile with the data it needs. Use the help and output the variables and Get-PnPFile output to determine what the Remove is looking for.

Hi Rob,

Below the scipt and output

# Variable - A modifier selon le contexte
$O365ServiceAccount="svc_.com"# Your User name  
$O365ServiceAccountPwd="ssss"#Your Password - You can use Get-credentials command as well  
$SharePointSiteURL="https://tractafric.sharepoint.com/Intranet/Projets" # Change this SharePoint Site URL
$Logfile="C:\pipe\files\testthomas\log\log_testthomas.txt"
$StartTime = Get-Date # Récupère la date
 
$SharedDriveFolderPath="C:\pipe\files\testthomas"  
$SharePointFolderPath="Documents/Extractions_Test"  

$env:PNPLEGACYMESSAGE='false'

# Sécurisation du mot de passe lors de la connexion  
[SecureString]$SecurePass = ConvertTo-SecureString $O365ServiceAccountPwd -AsPlainText -Force  
[System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass) 
 
# Connexion au site SharePoint Online  
Connect-PnPOnline -Url $SharePointSiteURL -Credentials $PSCredentials  

# Lecture des fichiers dans SharePoint Document Library Folder /Extractions_Test
$Files=Get-PnPFolderItem -FolderSiteRelativeUrl $SharePointFolderPath -ItemType File


# Wait for 5 seconds
Start-Sleep -Seconds 5


foreach ($File in $Files) {
    # Télécharge les fichiers to Network Shared Path et log le nom des fichiers
    "Downloading File $($File.Name)" | Add-Content -Path "C:\pipe\files\testthomas\log\log_testthomas-$($StartTime.ToString("yyyyMMdd")).txt"
    Get-PnPFile -Url $File.ServerRelativeUrl -Path $SharedDriveFolderPath -FileName $File.Name -AsFile
    
} 

$pnpFile =  Get-PnPFile -Url $File.ServerRelativeUrl -Path $SharedDriveFolderPath -FileName $File.Name -AsFile

#Show output to see if the data looks correct for troubleshooting    
'Deleting file {0}' -f $pnpFile.ServerRelativeUrl
    #Delete the Sharepoint files
if ($pnpFile) {
    Remove-PnPFile -ServerRelativeUrl $pnpFile.ServerRelativeUrl
}

Output:

PS C:\Windows\system32> C:\PS\CopyFromSharePointToLocal_TESTThomas_V1 - Copy.ps1
WARNING: File ‘Classeurtest.xlsx’ exists already. Use the -Force parameter to overwrite the file.
WARNING: File ‘Classeurtest.xlsx’ exists already. Use the -Force parameter to overwrite the file.
Deleting file

PS C:\Windows\system32>

but the file is not deleted on Sharepoint

The code and sample are guessing out how to help you. Trying to show you how to check variables and get things glued together to build what the command is looking for. There are example and tips all of the internet. The second link shows the format for deleting files.

Why Use Remove-PnPListItem vs Remove-PnPFile? – ThreeWill
Managing SharePoint Online Files with PowerShell | Petri IT Knowledgebase

Thanks for you’re help Rob, I’ll read and test on my side.