Using PnP Powershell, retreive a document's path
-
01-01-2021 - |
Question
I'm using PnP Powershell (which I'm new to) and I wish to find the URL of a document in a document library. The following returns Microsoft.SharePoint.Client.ObjectPathIdentity rather than an actual value. Here's my code:
$url = "https://tenant.sharepoint.com/teams/test"
Connect-PnPOnline –Url $url –Credentials (Get-Credential $userCredential)
$web = Get-PnPWeb
$ctx = Get-PnPContext
$libs = Get-PnPList | Where{$_.BaseTemplate -eq 101}
foreach($lib in $libs)
{
$libitems = Get-PnPListItem -List $lib -Fields "FileLeafRef"
foreach($libitem in $libitems)
{
if({$_.Name -match ".doc*"})
{
write-host "lib path... "$libitem.Path
}
}
}
The output is:
lib path... Microsoft.SharePoint.Client.ObjectPathIdentity followed by the Id, Title and Unique Id of all library items in the library.
Any thoughts? I was expecting the Path of the list item to be returned.
La solution
You need to use $libitem["FileRef"]
.
So, modify your inner foreach
loop as below:
foreach($libitem in $libitems)
{
if({$_.Name -match ".doc*"})
{
write-host "lib path... " $libitem["FileRef"]
}
}
It gives you the server-relative path of the file.
Also,if you just want the file name, use $libitem["FileLeafRef"]
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange