PowerShell’s 248 or 260 character limit path bug

Thanks to bad, old code, Microsoft’s PowerShell breaks on file or directory paths longer than 248 characters. PowerShell reuses other code that maintains compatibility with very old software that can’t understand paths with more than 260 characters. I don’t know how 260 drops to 248 in PowerShell, but it does.

Amazingly, in the first comment in a bug report, Microsoft dodges the question and passes the buck.

I hit this bug when working with a Sitecore web site. For example, I have a path like this:

C:\XXXXXXXXXXXXXXX\raw\WebSite\App_Data\MediaFiles\{11111111-1111-1111-1111-111111111111}\{3D6658D8-A0BF-4E75-B3E2-D050FABCF4E1}\{15451229-7534-44EF-815D-D93D6170BFCB}\{700C2C14-6082-4378-AA43-821E8422E9BE}\{6507E0E5-6CF2-4342-A11F-68F787B32EA3}Boulevard.jpg

That is 259 characters. I can’t delete it with PowerShell’s Remove-Item command.

Fortunately, there is a workaround: use legacy command prompt tools. In my case, I am trying to remove everything below C:\XXXXXXXXXXXXXXX\raw\, so I can use this command in PowerShell:

cmd /c rmdir C:\XXXXXXXXXXXXXXX\raw\ /s/q

But I shouldn’t have to do this. There is no reason that PowerShell can’t delete files with more than 248 character paths.

11 thoughts on “PowerShell’s 248 or 260 character limit path bug”

  1. RMDIR is also limited to MAX_PATH (260 chars), so it is not really a solution to the problem of how to remove files/directories with long paths. It just gets you 12 characters further in PowerShell.

  2. I’m a french user and i’have the same problem.
    But when I use “Map network Drive” I can use a Drive , mapping to folder with long path (Under 248 caracters) .
    Ex :
    Drive D:
    Folder : Abcdef ………..\agft …..\……….
    After under Windows Explorer i can delete file and folders under Drive D (in this exemple).
    Under DOS it’s works too.
    In poweshell I try to use PS-Drive to map a Folder with long path (Under 248 caracters) and after deleting files (like with windows).
    It’s doesn’t work.

    I received the bul….. message :
    “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”.
    Where is my mistake ?
    Is it work or not.

    1. I’m happy to know that.
      But may we have a link to see this wonderful tools.

      Regards.

  3. Try use:
    Remove-Item “\\?\C:\XXXXXXXXXXXXXXX\raw\WebSite\App_Data\MediaFiles\{11111111-1111-1111-1111-111111111111}\{3D6658D8-A0BF-4E75-B3E2-D050FABCF4E1}\{15451229-7534-44EF-815D-D93D6170BFCB}\{700C2C14-6082-4378-AA43-821E8422E9BE}\{6507E0E5-6CF2-4342-A11F-68F787B32EA3}Boulevard.jpg” -Force

Leave a Reply

Your email address will not be published. Required fields are marked *