Sharepoint Copy - Object Id is invalid

I’m trying to move files from one SharePoint folder to another within the same site. My file reads a .csv file for the file name and URL to which the file is located. When I run the script, it asks me if I want to copy it over. After saying ‘Y’ I receive an error:

The object id “site:b4e2d7a7-aa0c-4fd1-8927-a7f5a7baf738:web:1e455200-ed00-41c7-8cf3-ba1dde:file:/
https://test123.sharepoint.com/Shared Documents/Duplicate Test Folder 1/Duplicate Test File (1).docx” is invalid.
At line:21 char:5

  • Copy-PnPFile -SourceUrl $SourceFileFullURL -TargetUrl $TargetFold ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : WriteError: (:slight_smile: [Copy-PnPFile], ServerException
    • FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.CopyFile

Here’s my code:

Connect to SharePoint Online

$SiteURL = “https://test123.sharepoint.com

Connect-PnPOnline -Url $SiteURL -UseWebLogin

Path to the CSV file

$CSVFilePath = “C:\Temp\Testduplicates.csv”

Import the CSV file

$CSVFile = Import-Csv -Path $CSVFilePath

Loop through each row in the CSV and copy the file

foreach ($Row in $CSVFile) {

$SourceFileURL = $Row.URL  # Column name in CSV for source file URL

$FileName = $Row.FileName  # Column name in CSV for file name

$TargetFolderURL = "/Shared Documents/Duplicate Backup"

# Construct the full source file URL
$SourceFileFullURL = $SiteURL + $SourceFileURL

# Construct the full target file URL
$TargetFolderURL = $SiteURL + $TargetFolderURL

# Construct the source URL
$SourceURL = $SiteURL + $SourceFileURL

# Copy the file
Copy-PnPFile -SourceUrl $SourceFileFullURL -TargetUrl $TargetFolderURL

}

Disconnect the session

Disconnect-PnPOnline

I’ve been working on this for about 8 hours and there’s not much information on object ID being invalid. Not sure what I’m doing wrong up to this point. I’m fairly new at powershell/sharepoint online management.

Hi and welcome to the forum. First things first, you should always format your code using the “preformatted text” option when posting. Even for error messages. It makes things easier to read when people are trying to help. E.g.

#Connect to SharePoint Online
$SiteURL = "https://test123.sharepoint.com"
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Path to the CSV file
$CSVFilePath = "C:\Temp\Testduplicates.csv"

#Import the CSV file
$CSVFile = Import-Csv -Path $CSVFilePath
#Loop through each row in the CSV and copy the file
foreach ($Row in $CSVFile) {
    $SourceFileURL = $Row.URL  # Column name in CSV for source file URL
    $FileName = $Row.FileName  # Column name in CSV for file name
    $TargetFolderURL = "/Shared Documents/Duplicate Backup"
    # Construct the full source file URL
    $SourceFileFullURL = $SiteURL + $SourceFileURL
    # Construct the full target file URL
    $TargetFolderURL = $SiteURL + $TargetFolderURL
    # Construct the source URL
    $SourceURL = $SiteURL + $SourceFileURL
    # Copy the file
    Copy-PnPFile -SourceUrl $SourceFileFullURL -TargetUrl $TargetFolderURL
}
#Disconnect the session
Disconnect-PnPOnline

and your error message says:

The object id "site:b4e2d7a7-aa0c-4fd1-8927-a7f5a7baf738:web:1e455200-ed00-41c7-8cf3-ba1dde:file:/
https://test123.sharepoint.com/Shared Documents/Duplicate Test Folder 1/Duplicate Test File (1).docx" is invalid.
At line:21 char:5
Copy-PnPFile -SourceUrl $SourceFileFullURL -TargetUrl $TargetFold ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CategoryInfo : WriteError: (:slight_smile: [Copy-PnPFile], ServerException
    FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.CopyFile

I think i’ve used the PnP module like once, so I’m no expert but it looks like your total $SourcefileFullUrl according to this error message is:

"site:b4e2d7a7-aa0c-4fd1-8927-a7f5a7baf738:web:1e455200-ed00-41c7-8cf3-ba1dde:file:/https://test123.sharepoint.com/Shared Documents/Duplicate Test Folder 1/Duplicate Test File (1).docx"

Which doesn’t look like a valid SourceUrl to me. The documentation says that SourceUrl should be a relative URL specifying the file or folder to copy. Your Url mentioned in the error message is not relative as it contains a full URL with https, the domain and everything. Same thing for the $TargetFolderUrl. It should be relative and not include the actual site/domain in it.

Thanks. I’ll make sure next time to format the code better,

It’s not too late to edit your post :slight_smile:

1 Like