0

I have the following PnP PowerShell to get the URL of a file and remove the ".lock" string from the file name using Replace:-

$url = $item.File.ServerRelativeUrl
Write-Host "$(Get-Date) - ($($Counter)/$($AllItems.Count)) - Restoring version $VersionLabel for $($Item.File.Name)"
$item.File.Versions.RestoreByLabel($VersionLabel)
Invoke-PnPQuery
                
$restoedFile = Get-PnPFile -Url $url 
Write-Host "2) -->"
            
url2 = $url.Replace(".lock","")
Write-Host "1) $url" $url
Write-Host "3) $url2" $url2 

But this code url2 = $url.Replace(".lock","") will return empty string. Any advice?

1
  • 2
    In line url2 = $url.Replace(".lock","") in this line I cannot see $ for variable url2. It may be throwing exception. It should be $url2 = $url.Replace(".lock","") Mar 15 at 6:20

1 Answer 1

0

Try the below code which will get the files and then get each file name without extension:

$LibraryName = "Documents"
$LibraryItems = Get-PnPListItem -List $LibraryName -PageSize 500 -Fields "FileLeafRef" | Where {$_.FileSystemObjectType -eq "File"} 

#Get File count
Write-Host "Total Number of Items Found:"$LibraryItems.count 

#Get each File name
ForEach($Item in $LibraryItems)
{
    $FileNameWithExtension = $Item.FieldValues["FileLeafRef"] 
    $FileNameWithOutExtension = $FileNameWithExtension.Substring(0, $FileNameWithExtension.LastIndexOf("."))
    Write-Host $FileNameWithOutExtension    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.