Get-PnPFile : The object specified does not belong to a list. At line:4 char:9 + $File = Get-PnPFile -Url $RelativePath -AsListItem }

Sai Panat 0 Reputation points

I wanted to detch files as object and cretaed relative path and use , but is not working in my work computer but works in personal computer in test enviornment


foreach($row in $hashTable){


$RelativePath= $path.Substring($root.length)

$File = Get-PnPFile -Url $RelativePath -AsListItem }

A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
8,551 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,170 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ling Zhou_MSFT 8,935 Reputation points Microsoft Vendor

    Hi @Sai Panat,

    Thank you for posting in this community.

    The object specified does not belong to a list means that SharePoint attempt to get an Item or a file (Specified by $RelativePath) that does not belong to the SPWeb.

    I suggest you compare the $RelativePath on your work computer with the one in your test environment.

    You can use the Write-Host $RelativePath command to output your URL on the command line.

    You can share your full PowerShell commands with me if your URLs match exactly but the problem persists. This will help me to better test your issue and most likely find out why the problem is occurring. Please be careful not to give out your private information.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

  2. Sai Panat 0 Reputation points

    foreach($row in $hashTable)





    $SitePath =”http://sitepath”


    $arr = @()





    If ($Checksite -eq $SitePath)


    $RelativePath= $path.Substring($root.length)       #Trim to get relative path





    $File = Get-PnPFile -Url $RelativePath -AsListItem  #Get File as an Object

    $RolePermissions = Get-PnPProperty -ClientObject $File -Property RoleAssignments #Get all users for the file

    $File.BreakRoleInheritance($True, $False) #Break Inheritance


    while($val -ne $RolePermissions.count) #Delete each user access






    $CSVFileFound = "C:\Desktop\New folder\Filefound.csv"

    $Path | Out-File $CSVFileFound  -Append






    $arr += [pscustomobject]@{Error=






    $CSVNOTINDOCONE = "C:\User\Filenotindocone.csv"

    $Path | Out-File $CSVNOTINDOCONE  -Append



    0 comments No comments