Get-Childitem Limitation of 260 characters. Is this the year 2018?

Powershell is getting better and better, but the limitations of only 260 character with Get-Childitem is hilarious when i see the comment at the “Topic Title” (Maximum Length: 80)

Am guessing there is a good reason, for the limitation…

Is there some workaround i can build into my script so it wil go to the path where/if error occurs and restart the scan with the same parameters?

 

The 260 character limit for paths is actually part of the Windows API.

If you’re running on anything more recent than Windows 10 version 1607, you can opt-in to a new behavior that does allow paths longer than 260 chars. Taken from “Naming a File”:

A registry key allows you to enable or disable the new long path behavior. To enable long path behavior set the registry key at `HKLM\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled (Type: REG_DWORD)`

The article also notes that you can set it via GPO, and that you may need a reboot to fully realize the benefits.

Having said that, if you’re relying on this feature and then publish scripts / functions / modules, it may be a fun issue to diagnose for unwary users!

Am using Windows Server 2012 R2. I want to achieve to get all hidden files from a fileserver. but i get the limitation 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.

At line:1 char:1

I gonna try to grep the error and use the path in the error as new starting point to execute the command again

You could use robocopy to “scan” the folders with the option “/L”, log the result to a file and parse this file with Powershell … that’s how I did it a while ago. :wink:

What version of PS do you have? Does using 5.1 or 6 make a difference? Or try the \?\C:\ syntax. Related thread: https://powershell.org/forums/topic/script-to-find-long-path-names/

It is version 4.0… :frowning:

You can use 3:rd party libraries.

Wrote a couple of blog posts about QuickIO.Net here:

https://quickio.net/

It was over a year ago since I wrote the last blog post so I can’t remember if the file attributes get captured.
QuickIO is no longer developed as far as I know, there are other libraries but can’t remember the names.

Does something like this work? powershell - Handling Path Too Long Exception with New-PSDrive - Stack Overflow

get-childitem -literalpath \\?\C:\ -recurse

This is one of the use cases I use PowerShell Core for!

Fixed PowerShell Core to allow use of paths that are more than 260 characters long. (#3960)
Source: https://docs.microsoft.com/en-us/powershell/scripting/whats-new/what-s-new-in-powershell-core-60?view=powershell-6

 

This is the related Github issue that John’s Microsoft doc alludes to. Great find, if it’s acceptable to get this running on OP’s server.

https://github.com/PowerShell/PowerShell/pull/3960

PS 5.1 has the same problem. Have you tried putting '\?' in front of the path? I happen to have a file whose path is 265 characters. It will say “Cannot find path” without the prefix.

PS C:\> $a = ls \\?\C:\WINDOWS\SoftwareDistribution\Download\ae84ea9d8e2d5b57697fdac2a81468b1\amd64_Microsoft-Windows-NetFx3-OnDemand-Package~~AMD64~~10.0.17134.1\amd64_netfx-aspnet_installsqlstatetemp_b03f5f7f11d50a3a_10.0.17134.1_none_ff11cd80956b65aa\installsqlstatetemplate.sql

PS C:\> $a


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


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


PS C:\> $a.tostring()
\\?\C:\WINDOWS\SoftwareDistribution\Download\ae84ea9d8e2d5b57697fdac2a81468b1\amd64_Microsoft-Windows-NetFx3-OnDemand-Package~~AMD64~~10.0.17134.1\amd64_netfx-aspnet_installsqlstatetemp_b03f5f7f11d50a3a_10.0.17134.1_none_ff11cd80956b65aa\installsqlstatetemplate.sql

PS C:\> $a.FullName.length
265


PS C:\> ls C:\WINDOWS\SoftwareDistribution\Download\ae84ea9d8e2d5b57697fdac2a81468b1\amd64_Microsoft-Windows-NetFx3-OnDemand-Package~~AMD64~~10.0.17134.1\amd64_netfx-aspnet_installsqlstatetemp_b03f5f7f11d50a3a_10.0.17134.1_none_ff11cd80956b65aa\installsqlstatetemplate.sql

ls : Cannot find path 'C:\WINDOWS\SoftwareDistribution\Download\ae84ea9d8e2d5b57697fdac2a81468b1\amd64_Microsoft-Window
s-NetFx3-OnDemand-Package~~AMD64~~10.0.17134.1\amd64_netfx-aspnet_installsqlstatetemp_b03f5f7f11d50a3a_10.0.17134.1_non
e_ff11cd80956b65aa\installsqlstatetemplate.sql' because it does not exist.
At line:1 char:1
+ ls C:\WINDOWS\SoftwareDistribution\Download\ae84ea9d8e2d5b57697fdac2a ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\WINDOWS\Soft...atetemplate.sql:String) [Get-ChildItem], ItemNotFound
   Exception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Confirmed that it works more easily in PS 6, which is portable.

PS C:\> pwsh
PowerShell 6.1.0
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS C:\> ls C:\WINDOWS\SoftwareDistribution\Download\ae84ea9d8e2d5b57697fdac2a81468b1\amd64_Microsoft-Windows-NetFx3-OnDemand-Package~~AMD64~~10.0.17134.1\amd64_netfx-aspnet_installsqlstatetemp_b03f5f7f11d50a3a_10.0.17134.1_none_ff11cd80956b65aa\installsqlstatetemplate.sql


    Directory: C:\WINDOWS\SoftwareDistribution\Download\ae84ea9d8e2d5b57697fdac2a81468b1\amd64_Microsoft-Windows-NetFx3
    -OnDemand-Package~~AMD64~~10.0.17134.1\amd64_netfx-aspnet_installsqlstatetemp_b03f5f7f11d50a3a_10.0.17134.1_none_ff
    11cd80956b65aa


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