Error while coping file from one document library to other

Copper Contributor

PS C:\Windows\System32> $target = "/Destination/"
PS C:\Windows\System32> $source = $File.FieldValues.FileRef
PS C:\Windows\System32> $source
/sites/####/Source/Leave Policy.pdf
PS C:\Windows\System32> $targetFile = $target + $File.FieldValues.FileLeafRef
PS C:\Windows\System32> $targetFile
/Destination/Leave Policy.pdf
PS C:\Windows\System32> Copy-PnPFile -SourceUrl $source -TargetUrl $targetFile -Force -ErrorAction Stop



Hi there I am trying to copy file using the above method but getting this error message


"Copy-PnPFile: {"odata.error":{"code":"-2147213275, Microsoft.SharePoint.Deployment.SPMigrationQosException","message":{"lang":"en-US","value":"Failed to verify the existence of destination location at ' Policy.pdf' due to error 'The system cannot find the file specified. (Exception from HRESULT: 0x80070002)'."}}}"


I am not sure why it's not adding "

sites/SiteName" in the target URL .... I tried to do it by adding manually but not working ...... any suggestion would be welcoming.

2 Replies



You can provide either a full URL or a relative address, but it's up to you to ensure it's valid. SharePoint - like any other web platform - won't "guess" at what you might like to have pre- or appended.


If you provide a full URL, SharePoint will attempt to use that explicit address and throw an exception if a bad URL value has been passed in.


Similarly, you can provide a relative URL and SharePoint will prepend the protocol and host name components, but that's all - at least in the case where your relative URL begins with a forward slash, as your "/Destination/" specification does. This is common behaviour across web services and not specific to SharePoint.


What you can't do - which is what you're asking about - is provide only part of a relative URL and expect the service on the other end to know precisely what you intended. Relative addressing just doesn't work like that (you guessed it: this is not unique to SharePoint.)


The path of least resistance is simply to fix up your first string assignment to $target so that "/Destination/" is changed to the proper relative URL, for example, "/sites/siteName/Destination/". Alternatively, you can get fancy and script the calculation of the missing parts, but however you choose to do it, it has to result in a properly-formatted full or relative URL - SharePoint isn't going to fill in the blanks for you.


The error you're receiving is quite valid in this case.


As an aside (and at the risk of sounding droll), this also has nothing to do with the PnP SharePoint module, either. This is standard web addressing concepts.


Note: Since you're including the file name in the TargetUrl, be aware of the limitations around copying across sites or to a subsite - as per the official GitHub reference below. Maybe you're already aware of this but it's worth a shout out for anyone who isn't.


Copy-PnPFile | PnP PowerShell


There's also some good examples in this article that show in what specific situations you can leave parts out (related to the current site, which in turn is related to having established a PSDrive), or in which others are required.




@LainRobertson thanks for replying... yes you are right that I had to give proper relative URL , but it should not have the last forward slash... such as instead for this "/sites/siteName/Destination/", it should be "/sites/siteName/Destination" this. :)