Find paths longer than 260 characters

Last updated:

#WINDOWS

When running Get-ChildItem or Get-Acl, we might come across this issue.

In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters.


# This gets everything, we can optionally just search for directories as well
Get-ChildItem –Force –Recurse –ErrorAction SilentlyContinue –ErrorVariable AccessDenied

# Then find the paths using this error variable
$AccessDenied |
Where-Object { $_.Exception -match "must be less than 260 characters" } |
ForEach-Object { $_.TargetObject }

references:

Naming Files, Paths, and Namespaces - Win32 apps | Microsoft Learn Maximum Path Length Limitation - Win32 apps | Microsoft Learn