Remove-Item: the specified path, file name are too long

This topic contains 8 replies, has 7 voices, and was last updated by Profile photo of Hunter Wyatt Hunter Wyatt 7 months ago.

  • Author
  • #33208
    Profile photo of Mariuxi Pacheco
    Mariuxi Pacheco

    We have many deep long directory structure that we need to delete but the file path is too long to delete. how can I work around this with PowerShell. Any ideas.
    Remove-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260
    characters, and the directory name must be less than 248 characters.
    At line:1 char:28
    + Get-ChildItem | Remove-Item <<<< + CategoryInfo : WriteError: (Z:\Folder:String) [Remove-Item], PathTooLongException + FullyQualifiedErrorId : RemoveItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

  • #33211
    Profile photo of Don Jones
    Don Jones

    The 255-character limit is actually embedded in .NET, which PowerShell is written in, so you can't really work around it. You could potentially call Cmd.exe, and have it run the Del command – that gets you out of PowerShell. Another option is to map a PSDrive (New-PSDrive) somewhere into your path, so that instead of deleting C:\Really\Long\Path\That\Will\Not\Work, you could map Short: to \Really\Long\Path and then delete Short:\That\Will\Not\Work instead. I'd suggest the latter approach – mapping a drive into the directory structure.

  • #33369
    Profile photo of Mariuxi Pacheco
    Mariuxi Pacheco


  • #35470
    Profile photo of phenix82

    File name too long is a common issue, and I also had it before, but I figured out under the help of long path tool.

  • #35471
    Profile photo of Bob McCoy
    Bob McCoy

    Don, this comes up so often, maybe it needs to be added to "The Big Book of PowerShell Gotchas."

  • #35472
    Profile photo of Graham Beer
    Graham Beer

    Could you not use rename-item as part of a script to remove the file ? For example if you do a count on the file and if bigger than 255 in character length rename to a shortened name and if you have more than one file with this length increment a digit each time.
    Then remove the file/files.
    Not sure if its possible as a workaround, not had chance to test.

  • #35474
    Profile photo of Graham Beer
    Graham Beer

    Yes that works, just tried it. Put the long file name into a variable.

    So in my example i created a 255 character text file called 'aaaaaaaaaaa...txt", you get the gist !!

    $long = gci "aaaaaa*.txt" which of course puts the file into a variable.

    Then did the rename,
    Rename-Item $long -NewName Smaller.txt

    Which allowed me to remove the file,
    Remove-Item "smaller.txt"

    As i said, if you have more than one could wrap them in a foreach loop and increment them.

  • #35485
    Profile photo of David Schmidtberger
    David Schmidtberger

    i utilize alphafs for this task, here is the quick script i wrote to utilize an input file to delete multiple folders that exceed the 255 path

    $path         = Split-Path -parent $MyInvocation.MyCommand.Definition
    #insert full path to the alphafs.dll file for the import-module
    Import-Module AlphaFS.2.0.1\lib\net45\AlphaFS.dll
    $input  = $path + "\FolderDelete.csv"
    $csv = Import-Csv $input
    $log = $path + "\delete_log.txt"      
    Start-Transcript $log -append
    ForEach ($item in $csv)
    		$path = $item.path
    		Write-Host "Deleting $($item.path)"
    		[Alphaleonis.Win32.Filesystem.Directory]::Delete($path, $true, $true)
    		Write-Host "Error deleting $($item.path)"
  • #36930
    Profile photo of Hunter Wyatt
    Hunter Wyatt

    Long Path Tool

    Long path tool is the best solution for your problem. try it and solve your problem.

You must be logged in to reply to this topic.