Problem
Receiving PathTooLongException - ... fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
OS
PowerShell Version
.NET FrameWork Version
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.
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.