Find long folder and file paths with PowerShell


Often during server migrations we come across an issue when using Windows Sync Centre to sync up staff devices in that the path limit is 260 chars, using this simple one line we are able to find the paths that are too long for Sync Centre to sync up and rename them.

Once the folders have been renamed you can use takeown and icacls to set the permissions see here.

Open an Administrative PowerShell window, CD to the directory in question and run the following command.

Get-ChildItem -Recurse -Force -ErrorAction SilentlyContinue | Where-Object {$_.FullName.Length -gt 250}

NB – You can address long file paths with icacls using the below

Prefixing my path with the string “\\?\” as below, allows icacls to successfully address longer file paths.

icacls “\\?\M:\Archive\*” /c /t /reset

When my script runs using this prefix, it now runs correctly, and successfully processes all files without error….SUCCESS!!! This also works with UNC paths.

Found priceless insights in this blog? Support the author’s creativity – buy them a coffee!

3 thoughts on “Find long folder and file paths with PowerShell

  1. How would UNC syntax work exactly? When I use a syntax like:
    icacls “\\?\\\fs.domain.local\share\archive\*” /c /t /reset
    it returns an error.

    1. The UNC (Universal Naming Convention) syntax is used for accessing network resources, like shared files or printers, in a networked environment. The basic format of a UNC path is `\\servername\sharename\path\filename`.

      In your example, you are using the `icacls` command with a UNC path, but there seems to be an issue with the path formatting. The `icacls` command is used to modify the Access Control Lists (ACLs) for files and directories in Windows, including those on network shares.

      The UNC path you provided: `\\?\fs.domain.local\share\archive\*` appears to have an incorrect format. The `\\?\` prefix is typically used to specify an extended-length path, but it’s not usually combined with UNC paths. Instead, a standard UNC path should be sufficient for your needs.

      Your command should look more like this:
      icacls “\\fs.domain.local\share\archive\*” /c /t /reset

      Here’s what each part does:

      1. `\\fs.domain.local\share\archive\*`: This is the UNC path to the folder whose permissions you want to reset. The `*` at the end indicates that the command should apply to all files within that directory.
      2. `/c`: This option tells `icacls` to continue on file errors. This can be useful in a network environment where some files might be inaccessible.
      3. `/t`: This option allows the command to work recursively through subdirectories.
      4. `/reset`: This resets the ACLs of the files/directories to their default values.

      Remember to run the command prompt as an administrator to ensure it has the necessary permissions to modify file ACLs on a network share. Also, ensure that the path `\\fs.domain.local\share\archive` is accessible and correct, and that you have the necessary permissions on the network share.

Leave a Reply

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