You’re running a script, perhaps to update files in a SharePoint document library, and suddenly, your process grinds to a halt with the frustrating message: “Error: The file A has been modified by A on A.” This error is a common indicator that the file you’re trying to work with is locked or has been changed by another user or process simultaneously.

What’s Causing the Conflict?

This error typically pops up when your script, for example, a PowerShell script using Set-PnPListItem to update SharePoint files, detects that the file’s state has changed since it was last accessed. Imagine this scenario: your script reads a file’s metadata (Get-PnPFile), prepares to update it, but in that brief interval, another user, an automated process, or a synchronization service (like OneDrive) modifies or saves a new version of the same file. When your script then tries to apply its changes (Set-PnPListItem), SharePoint or the underlying system flags this discrepancy, leading to the error.

The problem is often seen in environments where files are shared or subject to real-time synchronization.

Here’s a conceptual look at code that might run into this issue:

# Pseudocode illustrating the problem
# Get the file from SharePoint
$file = Get-PnPFile -Url "/sites/MySite/Shared Documents/MyFile.docx"

# ... some processing happens here ...

# Attempt to update the list item associated with the file
# This is where the error might occur if the file was modified externally
Set-PnPListItem -List "Shared Documents" -Identity $file.ListItemAllFields.Id -Values @{"MyColumn" = "New Value"}
PowerShell

How to Resolve the “File Modified” Error

When you encounter this error, the primary goal is to prevent these concurrent modifications while your script is operational.

Immediate Fixes

Based on common causes, here are some steps you can take:

  1. Pause Synchronization Services: If you’re using tools like Microsoft OneDrive, Google Drive, or similar real-time file synchronization services, pause their activity while your script runs. These services can automatically update files in the background, leading to the conflict.
  2. Wait for Updates: If Microsoft Office applications are updating or if there are pending Office updates, it’s best to let them complete before running your script. These updates can sometimes lock files or modify them.
  3. Manage File Access:
    • If you’ve granted access to the specific files your script is working on to other users, consider temporarily revoking this access.
    • Similarly, if the folders containing these files are shared, you might need to temporarily unshare them to ensure your script has exclusive access during its execution.

Also Read:

Alternative and Proactive Solutions

Beyond immediate manual interventions, consider these more robust approaches:

1. Implement Retry Logic in Your Script:
Modify your script to catch this specific error and retry the operation after a short delay. This can often resolve temporary conflicts.

# Conceptual PowerShell retry logic
$retryCount = 3
$delaySeconds = 5
$success = $false

for ($i = 0; $i -lt $retryCount; $i++) {
    try {
        # Your Get-PnPFile and Set-PnPListItem operations here
        # e.g., Set-PnPListItem -List "Documents" -Identity 1 -Values @{"Title"="NewTitle"} -ErrorAction Stop
        Write-Host "Attempt $($i+1): Update successful."
        $success = $true
        break
    }
    catch [System.Management.Automation.CmdletInvocationException] {
        if ($_.Exception.Message -match "The file .* has been modified by .* on .*") {
            Write-Warning "Attempt $($i+1): File modified error. Retrying in $delaySeconds seconds..."
            Start-Sleep -Seconds $delaySeconds
        } else {
            # Handle other errors
            Write-Error "An unexpected error occurred: $($_.Exception.Message)"
            break
        }
    }
    catch {
        Write-Error "A non-CmdletInvocationException occurred: $($_.Exception.Message)"
        break
    }
}

if (-not $success) {
    Write-Error "Failed to update the file after $retryCount attempts."
}
PowerShell

2. Check File Lock Status: Before attempting an update, see if you can programmatically check if a file is checked out or locked. SharePoint’s PnP PowerShell cmdlets might offer ways to inspect the checkout status of a file (e.g., Get-PnPFile can return checkout information). If a file is checked out, you might need to wait or handle it accordingly.

3. Ensure Proper Permissions and Session Management: Double-check that the account running the script has the necessary permissions. Sometimes, intermittent connectivity or session expiry issues with SharePoint Online can manifest in unexpected ways, though this specific error is more directly tied to concurrent modification.

Conclusion

The “Error: The file A has been modified by A on A” is a common hurdle when scripting interactions with actively used file repositories like SharePoint. By understanding that it signals a concurrent modification and by taking steps to either pause conflicting services or build resilience into your scripts, you can significantly reduce the occurrence of this issue and ensure your automated tasks run more smoothly.