Script to find long path names

Hello,

I am trying to find a PowerShell script that will produce a list of all files\folders where the total path is more than 260 characters.

I see the following but it doesn’t seem to work.
https://www.powershellmagazine.com/2012/07/24/jaap-brassers-favorite-powershell-tips-and-tricks/
 
Dir d:\data\FolderName Get-ChildItem | Where-Object {$.FullName.Length -gt 260}
I also tried Get-ChildItem | Where-Object {$
.FullName.Length -gt 260} from How do I find files with a path length greater than 260 characters in Windows? - Stack Overflow but that does work.

Can someone let me know what I am missing?

thanks,

Roger

 

Try the solution provided here:

With recent version of Windows, you can set a registry change to deal with long file names.

https://blogs.msdn.microsoft.com/jeremykuhne/2016/07/30/net-4-6-2-and-long-paths-on-windows-10

This?

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

Hi Js, Sorry that doesn’t appear to work.

Roger,

When you say it doesn’t work, what do you mean? Is it giving you an error? If so, can you please post. If it is providing incorrect results can you elaborate?

 

Using that script provides no results. It doesn’t error out but nothing happens.

If you want to search from the top folder on the c drive, c:, it would be (or cd to it first):

Get-ChildItem C:\ -Recurse -Force -ErrorAction SilentlyContinue | 
  Where-Object {$_.FullName.Length -gt 260}

Sorry I am really a beginner. I modified your command to try to get a list of files in a directory that exceed 260 characters as follows after selecting: cd d:
Get-ChildItem D:\Data\”Communications Team” -Recurse -Force -ErrorAction SilentlyContinue | Where-Object {$_.FullName.Length -gt 260}
After a few seconds the PS D:> prompt returns but doesn’t do anything or produce any results. What am I missing?

 

Get-ChildItem isn’t coded to allow for longer paths, from what I recall. So it won’t find anything.

What you can do is grab the NTFSSecurity module (Import-Module NTFSSecurity), which contains a Get-ChildItem2 cmdlet. It works the same as Get-ChildItem, but utilises a third party DLL to handle longer paths as well.

It works for me. What if you take out the “-erroraction silentlycontinue” option? Is there an error message?

    Directory: C:\WINDOWS\SoftwareDistribution\Download\ae84ea9d8e2d5b57697fdac2a81468b1\amd64_Microsoft-Windows-NetFx3-OnDemand-Package~~AMD64~~10.0.17134.1\amd64_netfx-aspnet_installsq
    lstatetemp_b03f5f7f11d50a3a_10.0.17134.1_none_ff11cd80956b65aa


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       10/29/2017   9:09 PM          53903 installsqlstatetemplate.sql


    Directory: C:\WINDOWS\SoftwareDistribution\Download\ae84ea9d8e2d5b57697fdac2a81468b1\amd64_Microsoft-Windows-NetFx3-OnDemand-Package~~AMD64~~10.0.17134.1\amd64_netfx-aspnet_personali
    zation_sql_b03f5f7f11d50a3a_10.0.17134.1_none_bdf33e156afb2609


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       10/29/2017   9:12 PM           7489 uninstallpersonalization.sql


    Directory: C:\WINDOWS\SoftwareDistribution\Download\ae84ea9d8e2d5b57697fdac2a81468b1\amd64_Microsoft-Windows-NetFx3-OnDemand-Package~~AMD64~~10.0.17134.1\amd64_netfx-aspnet_uninstall
    persistsql_b03f5f7f11d50a3a_10.0.17134.1_none_53b314e56d2c48b4


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       10/29/2017   9:18 PM          10195 uninstallpersistsqlstate.sql

Hi JS,

Unfortunately, I get the following error.

PS D:\data> Get-ChildItem ”Communications Team” -Recurse | Where-Object {$_.FullName.Length -gt 260}
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.
At line:1 char:1

  • Get-ChildItem ”Communications Team” -Recurse | Where-Object {$_.FullName.Length …
  • CategoryInfo : ReadError: (D:\data\Communi…al Photos Files:String) [Get-ChildItem], PathTooLongExcept
    ion
  • FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

Have you tried PS 6? There’s something about long pathnames that might get backported to PS 5. Allow use of long paths by jeffbi · Pull Request #3960 · PowerShell/PowerShell · GitHub

There might be a long pathname syntax?

“When calling Windows native API to determine if an item exists, ensure the path is prepended with \?\ to allow for paths > 260 characters.”

EDIT:

Try this:

Get-ChildItem -literalpath \\?\C:\ -Recurse | 
  Where-Object {$_.FullName.Length -gt 260}

reference: powershell - Handling Path Too Long Exception with New-PSDrive - Stack Overflow

Btw what version of powershell do you have? I don’t have any problems in 5.1.16299.611.