r/PowerShell • u/Key-Research-6708 • Nov 25 '25
Question File Paths too long
I want to compare 2 directories contents to make sure a robocopy completed successfully, and windows is saying the filepaths are too long, even after enabling long files paths in GPEdit and in Registry and putting \\?\ or \?\ before the filepaths in the variables is not working either. is there any way around this issue?:
script:
$array1 = @(Get-ChildItem -LiteralPath 'C:\Source\Path' -Recurse | Select-Object FullName)
$array2 = @(Get-ChildItem -LiteralPath 'C:\Destination\Path' -Recurse | Select-Object FullName)
$result = @()
$array2 | ForEach-Object {
$item = $_
$count = ($array1 | Where-Object { $_ -eq $item }).Count
$result += [PSCustomObject]@{
Item = $item
Count = $count
}
}
Error received with above script:
Get-ChildItem : 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.
error with \\?\ before file paths: Get-ChildItem : Illegal characters in path.
7
u/cschneegans Nov 25 '25 edited Nov 25 '25
Did you use the LongPathsEnabled registry value? You might need to restart the computer for this setting to take effect. With this setting, you can create ridiculously long paths in PowerShell, like so:
mkdir -Path ("${env:TEMP}\" + 'foo\' * 8000)1
u/WhatThePuck9 Nov 25 '25 edited Nov 25 '25
How are you composing your \\ paths? UNC paths have a limit of over 30000 characters.
2
u/vip17 Nov 25 '25
try PowerShell Core 7.0+, it handles long file names and
\\?\automatically, unlike PowerShell 5.12
u/dragery Nov 25 '25
User Powershell 7, or Robocopy.
For Robocopy, do a directory compare:
robocopy "C:\Source\Path" "C:\Destination\Path" /E /L /NS /NC /NFL /NDL /NJH /NJS
1
u/brads-1 Nov 25 '25
Old school DOS guy here, have you tried using subst?
subst q: c:\somereallylongfilepath\moregobbleygook
subst r: c:\someotherreallylongfilepath\evenmoregobbleygook
1
u/japskunk Nov 25 '25
I had an issue with file paths too long in another application, I created a share folder deeper in the root of the files and copied them that way. It made the actual file name really short.
1
u/jarod1701 Nov 25 '25
Use this.
1
u/TheThirdHippo Nov 25 '25
Old school method I used before PowerShell was a thing. Map the folder using \127.0.01\C$\Long\folder\path\ to eliminate the longer part of the path. It’s not the answer for this sub, but is quick, simple and you can continue with your current process
-1
u/DerkvanL Nov 25 '25
There is a registry key that enables 4096 characters.
via: https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry