How to remove unwanted file extension in SharePoint document library

Watson, Adam 26 Reputation points
2020-11-03T14:35:30.207+00:00

Hello and thanks in advance for the help. I am attempting to remove extra extensions that were unintentionally added to documents in multiple SharePoint libraries. When i manually remove the extension ".easy2lock" the file becomes available. However, when i try to do this programmatically via powershell and mapping the library to a network drive it works, but not for folders with paths greater than 256. Does anyone know how to get around this in powershell or using SharePoint API/management shell?

These all work but not for paths greater than 256:

[string]$BadExtension = ".easy2lock"





Get-ChildItem -Filter "*$BadExtension" -Recurse | Rename-Item -NewName {$_.Name -replace $BadExtension, [string]::Empty}







Get-ChildItem -Filter *$BadExtension -Recurse | ForEach-Object {

    Rename-Item -LiteralPath "\\?\$($_.FullName)" -NewName ($_.name -replace $BadExtension)

}



Get-ChildItem -Filter *$BadExtension -Recurse | ForEach-Object {

    Rename-Item -LiteralPath "\\?\$($_.FullName)" -NewName $_.basename

}



Get-ChildItem -Filter "*$BadExtension" -Recurse |

    Select-Object Name,@{n='LiteralPath';e={"\\?\$($_.fullname)"}} |

        Rename-Item -NewName {$_.Name -replace $BadExtension}



Get-ChildItem -Filter "*$BadExtension" -Recurse |

    Select-Object Basename,@{n='LiteralPath';e={"\\?\$($_.fullname)"}} |

        Rename-Item -NewName {$_.baseName}
Microsoft 365 and Office | SharePoint Server | For business
Microsoft 365 and Office | SharePoint | For business | Windows
Microsoft 365 and Office | SharePoint Server | Development
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} vote

Accepted answer
  1. Baker Kong-MSFT 3,801 Reputation points
    2020-11-04T05:29:26.377+00:00

    Hi @Watson, Adam ,

    Do you have to rename the targeted files through the mapping drive? Is it acceptable to use pnp powershell?

    #Set Variables  
    $SiteURL = "https://abc.sharepoint.com/sites/s01"  
       
    #Connect to PNP Online  
    Connect-PnPOnline -Url $SiteURL -UseWebLogin  
      
    $files= Find-PnPFile -List "Documents" -Match *.png  
      
    foreach ($f in $files) {  
        $f.ServerRelativeUrl   
      
        $name= '0'+ $f.Name  // rename it with your own name  
      
        Rename-PnPFile -TargetFileName $name -ServerRelativeUrl $f.ServerRelativeUrl  
    }  
    

    Or

    Best Regards,
    Baker Kong


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    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.


4 additional answers

Sort by: Most helpful
  1. Sharath Kumar Aluri 3,071 Reputation points
    2020-11-03T15:42:00.653+00:00

  2. James R 1 Reputation point
    2020-11-03T18:41:06.337+00:00

    I believe in CA you can define blocked file types in the Security section of CA. Hopefully, after the breach the file types were immediately added to the block list.

    You might be able to get around the limit by using search, provided it has been configured and is running smoothly.

    This link is for SP2013 but it should be a similar PS script to run a search on later versions of SharePoint: https://nikcharlebois.com/get-search-results-in-sharepoint-2013-using-powershell/

    The search query you would want to use would be something like: Filename:*.easy2lock


  3. Watson, Adam 26 Reputation points
    2020-11-04T05:37:23.68+00:00

    PNP powershell would be acceptable. I will give this a shot. Will this be able to surpass the SharePoint limitations for renaming files greater than 256 characters? My other solutions are blocked by both windows OS as well as sharepoint. I will give this a try nonetheless and get back to you. thakns!


  4. Watson, Adam 26 Reputation points
    2020-11-06T14:58:55.773+00:00

    This worked thanks BakeKong so much!
    $SiteURL = "https://Mydomain-my.sharepoint.com/sites/mysitecollection"
    [string]$DeleteBadExtension = ".easy2lock_read_me"
    [string]$BadExtension = ".easy2lock"

    Connect to PNP Online

    Connect-PnPOnline -Url $SiteURL -UseWebLogin
    $renamefiles= Find-PnPFile -List "Documents" -Match *.easy2lock
    foreach ($f in $renamefiles) {
    $f.ServerRelativeUrl
    $name = '0'+ $f.Name
    echo "Rename :"$name
    $Newvalue = $name -replace ".easy2lock"
    Rename-PnPFile -TargetFileName $Newvalue -ServerRelativeUrl $f.ServerRelativeUrl -force
    #Remove-PnPFile -TargetFileName $Newvalue -ServerRelativeUrl $f.ServerRelativeUrl
    }
    $removefiles= Find-PnPFile -List "Documents" -Match *.easy2lock_read_me
    foreach ($f in $removefiles) {
    $f.ServerRelativeUrl
    $delname = '0'+ $f.Name
    echo "Del NAme :" $delname
    #$deleteFileValue = $name -replace ".easy2lock_read_me"
    Remove-PnPFile -ServerRelativeUrl $f.ServerRelativeUrl -Force
    }

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.