PowerShell 5.0 PathTooLongException Error

461 views Asked by At

Problem

Receiving PathTooLongException - ... fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

OS

  • Windows Server 2008 R2 Enterprise

PowerShell Version

  • PSVersion 5.1.14409.1018
  • BuildVersion 10.0.14409.1018
  • CLRVersion 4.0.30319.36543
  • WSManStackVersion 3.0
  • PSRemotingProtocolVersion 2.3
  • SerializationVersion 1.1.0.1

.NET FrameWork Version

  • 4.5.51209

Code

Get-ChildItem -Path $path -Directory -Recurse | Get-ACL
ForEach ($item in $pathmany) {
        $permissions = $item.access | where {$_.IdentityReference -like "*$target*"}
        $permissions.FullPath | where {$_.IdentityReference -like "*$target*"}
        if ($permissions.IdentityReference -like "*$target*") {
            $path = $item.Path.trim('Microsoft.PowerShell.Core\FileSystem::')
        }
}

What I have tried so far

1 - Set-Location "further into folder tree" (error)

2 - Subst TEMP "folder further into folder tree" (error)

3 - \computer name\"folder further into folder tree"\ (error)

4 - Using a 2019 server where I can edit the registry to "LongPathsEnabled = 1" I also tried to use the \?\UNC\ method and it returned the same error.

Also tried this code and still get the error

$folder = @{ Name = "\\?\UNC"}
$folder.path = "folder further into folder tree"

$folder | ForEach-Object {
     $item = Get-ChildItem -Path $_.Path -Directory -Recurse | Get-ACL
}

Asking my supervisor if an upgrade to PowerShell 6.0 or 7.0 is doable.

Any help or ideas will be appreciated.

1

There are 1 answers

0
Soulless Rekkr On BEST ANSWER

I am using the above script with PowerShell 7 and the code is able to retrieve the folders where I was getting the LongPathException error. However, I am getting a new error that I will post a another question for. The answer I was able to come up with and works for my problem is as follows. I was able to count 145,000+ folders in the folder tree L:\Depts.

Script Problem Error

Get-ChildItem -Path "L:\Depts" -Directory -Recurse

Install PowerShell 7.0 will correct the error

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.