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","") Commented Mar 15, 2023 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 you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.