# Get-ChildItem : The specified path, file name, or both are too long. The fully q

**URL:** <https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613>\
**Category:** PowerShell Help\
**Created:** [May 1, 2018, 5:31pm UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613 "2018-05-01T17:31:14Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![nlong12](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/nlong12/32/123_2.png) [@nlong12](https://forums.powershell.org/u/nlong12)\
**Post date:** [May 1, 2018, 5:31pm UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/1 "2018-05-01T17:31:14Z")

</div>

Good afternoon all!

Running the following one liner:  
$Results = Get-ChildItem -Path ‘I:\*.\*’ -Recurse -Force | Where-Object {$_.length/1GB -gt 15} | Select-Object Name,Extension,Fullname,CreationTime,LastAccessTime,@{n=”Size MB”;e={$_.length/1GB}}

Getting lots of the following error messages:  
Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260

Is there any way to get around these errors? I’ve been researching at the university of google, tried -LiteralPath '\?\I:' nothing that I’ve found has worked.

Any and all input would be greatly appreciated, this is over my head.

Norm

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [May 1, 2018, 6:12pm UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/2 "2018-05-01T18:12:29Z")

</div>

PowerShell leverages .NET which has the 256 path character limit. We can use something that leverages COM such as [robocopy.exe](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy)

This will not copy anything, just list the full file names under the given $FolderPath recursively…

```
$FolderPath = 'D:\Sandbox'

robocopy $FolderPath NULL /L /E /MT:128 /FP /NP /TEE /NJH /NJS /NC /NDL /R:0 /W:0 /XJ | foreach {
    $PSItem.Substring($PSItem.IndexOf(':') - 1 , $PSItem.Length - $PSItem.IndexOf(':') + 1)
}
```

---

<div class="post-metadata">

**Author:** ![nlong12](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/nlong12/32/123_2.png) [@nlong12](https://forums.powershell.org/u/nlong12)\
**Post date:** [May 1, 2018, 6:17pm UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/3 "2018-05-01T18:17:26Z")

</div>

Sam;  
Thank you for your input. Question how I tie together your code with my $Results = Get-ChildItem -Path ‘I:\*.\*’ -Recurse -Force | Where-Object {$_.length/1GB -gt 15} | Select-Object Name,Extension,Fullname,CreationTime,LastAccessTime,@{n=”Size MB”;e={$_.length/1GB}}

Norm

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [May 1, 2018, 6:25pm UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/4 "2018-05-01T18:25:38Z")

</div>

I’ll tell on one condition…  
Explain to me the 4 lines I posted above.

---

<div class="post-metadata">

**Author:** ![nlong12](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/nlong12/32/123_2.png) [@nlong12](https://forums.powershell.org/u/nlong12)\
**Post date:** [May 2, 2018, 11:20am UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/5 "2018-05-02T11:20:37Z")

</div>

Sam;  
Not sure I can pass your test I’m happier in the linux world with bash scripts but here goes:

1. Your using robocopy with applicable options (no knowledge of what each option is doing but I guess I can look them up) to the contents of $FolderPath
2. In the for each loop it appears you are using a substring method to return values in this case the path of every file contained in $FolderPath. Honestly not sure what your doing with the $PSItem.Length - $PSItem.IndexOf(‘:’) + 1). Powershell complained a bit when I tested the code: Exception calling “Substring” with “2” argument(s): “StartIndex cannot be less than zero.  
Parameter name: startIndex”
3. Ran the following in order to drill down into the object typename in order to attempt to figure out what you were doing  
$FolderPath | Get-Member  
$FolderPath.Substring | Get-Member

Do I pass?

Norm

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [May 2, 2018, 11:40am UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/6 "2018-05-02T11:40:36Z")

</div>

THere is an alternative. If you grab the NTFSSecurity module from the PSGallery:

```
Install-Module NTFSSecurity -Scope CurrentUser
```

Then you can use the Get-ChildItem2 cmdlet, which leverages a third-party .dll to access the filesystem and correctly handle longer paths. The syntax is identical to Get-ChildItem, and it supports all the same parameters, it just works with a different library to manage the long paths:

```
Get-ChildItem2 -Path $TargetPath_Long -Recurse -Force
```

---

<div class="post-metadata">

**Author:** ![nlong12](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/nlong12/32/123_2.png) [@nlong12](https://forums.powershell.org/u/nlong12)\
**Post date:** [May 2, 2018, 12:47pm UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/7 "2018-05-02T12:47:31Z")

</div>

Hi Joel, apparently the third-party dll does note like a PSDrive mount volume: New-PSDrive -Name “I” -PSProvider “FileSystem” -Root “\jesse\users”

When I run the following:  
$Results = Get-ChildItem2 -Path I:\ -Recurse | Where-Object {$_.length/1GB -gt 15} | Select-Object Name,Extension,Fullname,CreationTime,LastAccessTime,@{n=”Size GB”;e={$_.length/1GB}}

The following error messages occur  
Get-ChildItem2 : Unable to find the specified file.  
At line:1 char:12

- $Results = Get-ChildItem2 -Path I:\ -Recurse | Where-Object {$\_.lengt …
- 

```
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : ObjectNotFound: (I::String) [Get-ChildItem2], FileNotFoundException
  - FullyQualifiedErrorId : FileNotFound,NTFSSecurity.GetChildItem2

Don’t get these messages with Get-ChildItem ran against the created psdrive.

Any thoughts

Thanks for input  
Norm

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [May 2, 2018, 1:15pm UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/8 "2018-05-02T13:15:47Z")

</div>

Yes, the whole point is to further your understanding of PowerShell.

The use of the line

```
robocopy $FolderPath NULL /L /E /MT:128 /FP /NP /TEE /NJH /NJS /NC /NDL /R:0 /W:0 /XJ /BYTES
```

outputs the file size in bytes, and the full file name

This output is then piped to one or more lines to parse that text output of robocopy.exe to extract the Name,Extension,Fullname,CreationTime,LastAccessTime,FileSize properties you want.

What’s missing in the robocopy.exe output is the creationtime and lastaccesstime perperties.

I admit that the expression

```
$PSItem.Substring($PSItem.IndexOf(':') - 1 , $PSItem.Length - $PSItem.IndexOf(':') + 1)
```

is complex and not optimal for learning. But the idea is simple. Robocopy returns a line of text for each file that looks like:

```
12345 c:\bla1\bla 2\bla.3\bla4.txt
```

You need to parse out file name, extension, and full name. You have to account for possible multiple spaces and multiple dots (but a single colon)  
if

```
$a = ' 12345 c:\bla1\bla 2\bla.3\bla4.txt'
```

then

```
$a.Split(':')
```

will return

```
12345 c
\bla1\bla 2\bla.3\bla4.txt
```

The colon is an attractive marker for parsing here because you can be assured that there can be only one colon in a file name  
The .Split() method will provide an array of 2 elements.  
The second one is the file full name less the colon and the drive letter which is the last letter of the first split string.  
So,

```
$a.Split(':')[1]
```

returns the 2nd element of that .Split() array of text (array elements are counted starting from 0)

```
\bla1\bla 2\bla.3\bla4.txt
```

The last letter of the first .Split() array element can be referenced as

```
$a.Split(':')[0][-1]
```

This may be a little complicated, but simply put

```
$a.Split(':')[0]
```

returns the first string of characters being the first .Split() array element

```
12345 c
```

which is in itself an array of characters. The last array element can be refereed to by [-1]  
So, to stitch this expression together, the file full name can be parsed as:

```
"$($a.Split(':')[0][-1]):$($a.Split(':')[1])"
```

What this expression does is to concatenate the last character before the colon + the colon + the rest of the characters after the colon  
Once we have file full name as in

```
$FileFullName = "$($a.Split(':')[0][-1]):$($a.Split(':')[1])"
```

We can get the file name by using:

```
$FileName = Split-Path $FileFullName -Leaf
```

Using similar parsing techniques, we can demo parsing a line like:

```
$a = ' 12345 c:\bla1\bla 2\bla.3\bla4.txt'

$FileFullName = "$($a.Split(':')[0][-1]):$($a.Split(':')[1])"

$FileName = Split-Path $FileFullName -Leaf

$Extension = $FileName.Substring($FileName.LastIndexOf('.') , $FileName.Length - $FileName.LastIndexOf('.'))

[Int]$FileSize = ($a.Split(':')[0][0..($a.Split(':')[0].Length -2)] -join '').Trim()

"If the string is '$a'"
"FileFullName is '$FileFullName'"
"FileName is '$FileName'"
"Extension is '$Extension'"
"Size (bytes) is '$FileSize' bytes"
```

In the original expression $PSItem or $\_ refers to the text line being passed to the pipeline from robocopy.exe

Putting the parsed output in a more manageable PS object:

```
$FolderPath = 'D:\Sandbox'

$Results = robocopy $FolderPath NULL /L /E /MT:128 /FP /NP /TEE /NJH /NJS /NC /NDL /R:0 /W:0 /XJ /BYTES | foreach {
    $FileFullName = "$($PSItem.Split(':')[0][-1]):$($PSItem.Split(':')[1])"
    $FileName = Split-Path $FileFullName -Leaf
    [Int]$FileSize = ($PSItem.Split(':')[0][0..($PSItem.Split(':')[0].Length -2)] -join '').Trim()
    [PSCustomObject][Ordered]@{
        Name = $FileName
        Extension = $(
            if ($FileName -match '\.') { # this if statement accounts for files with no extension
                $FileName.Substring($FileName.LastIndexOf('.') , $FileName.Length - $FileName.LastIndexOf('.'))
            } else {
                'None' 
            }
        )
        FullName = $FileFullName
        'Size(GB)' = [Math]::Round($FileSize / 1GB , 2)
    }
}

$Results | FL
```

What’s missing here is the CreationTime and LastAccessTime, can you lookup robocopy documentation to see if there are switches to show those?

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [May 2, 2018, 1:32pm UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/9 "2018-05-02T13:32:11Z")

</div>

Strange. You may be able to bypass the issue by using the -Persist parameter of New-PSDrive, as that causes it to make a new network drive much like windows explorer does, although you’d have to manually remove it when you’re done.

It’s very possible that the third party DLL wasn’t designed for lengthy network paths, I’m not sure. I know there’s at least one other module that has a cmdlet for this purpose of a similar design, I think the cmdlet was called Get-LongChildItem if that helps you find the module at all. Perhaps that one works a bit differently.

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [May 2, 2018, 2:10pm UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/10 "2018-05-02T14:10:15Z")

</div>

Here’s a version with CreationTime and LastAccessTime and the file size filter

```
$FolderPath = 'D:\Sandbox'
$FilterFileSize = 15GB # return files larger than this value

$Results = robocopy $FolderPath NULL /L /E /MT:128 /FP /NP /TEE /NJH /NJS /NC /NDL /R:0 /W:0 /XJ /BYTES | foreach {
    if ($PSItem -match ':') {
        $FileFullName = "$($PSItem.Split(':')[0][-1]):$($PSItem.Split(':')[1])"
        $FileName = Split-Path $FileFullName -Leaf
        if ($FileName -match '\.') { # this if statement accounts for files with no extension
            $Extension = $FileName.Substring($FileName.LastIndexOf('.') , $FileName.Length - $FileName.LastIndexOf('.'))
        } else {
            $Extension = 'None' 
        }
        [Int]$FileSize = ($PSItem.Split(':')[0][0..($PSItem.Split(':')[0].Length -2)] -join '').Trim()
        if ($FileSize -ge $FilterFileSize) {
            [PSCustomObject][Ordered]@{
                FullName = $FileFullName
                Name = $FileName
                Extension = $Extension
                'Size(GB)' = [Math]::Round($FileSize / 1GB , 2)
                CreationTime = [IO.File]::GetCreationTime($FileFullName)
                LastAccessTime = [IO.File]::GetLastAccessTime($FileFullName)
            }
        }
    }
}

$Results | FL
```

---

<div class="post-metadata">

**Author:** ![nlong12](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/nlong12/32/123_2.png) [@nlong12](https://forums.powershell.org/u/nlong12)\
**Post date:** [May 2, 2018, 3:27pm UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/11 "2018-05-02T15:27:56Z")

</div>

Hello Sam;  
Thank you for the explanation and pushing me to do some digging. In running your final script came across the following error message:  
Cannot convert value “4268605440” to type “System.Int32”. Error: “Value was either too large or too small for an Int32.”  
At line:13 char:9

- 

```
    [Int32]$FileSize = ($PSItem.Split(':')[0][0..($PSItem.Split(' ...

```

- 

```
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : MetadataError: (🙂 , ArgumentTransformationMetadataException
  - FullyQualifiedErrorId : RuntimeException

Did some digging and poking around and looking and looking at the error message came to the realization of the need to change the Int definition to Int64. Still chewing on your script explanation.

Norm

---

<div class="post-metadata">

**Author:** ![fredrik-kacsmarck](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@fredrik-kacsmarck](https://forums.powershell.org/u/fredrik-kacsmarck)\
**Post date:** [May 3, 2018, 4:13am UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/12 "2018-05-03T04:13:34Z")

</div>

Haven’t checked but depending on your .Net version and OS version you may only need to change a setting in the registry.  
Just do a google/bing/duckduckgo on “windows maxpath” and you should find articles about the registry key and .net version.

You could also try third party .dll’s.  
I created a blog post about using [quickio.net](http://quickio.net) some time ago.  
[QuickIO.Net and Powershell](http://psfredrik.chiloma.com/2016/12/15/using-quickio-net-with-powershell/)

QuickIO don’t seem to be developed further so YMMV.  
But in my opinion it’s a “nicer” approach to get the results as objects directly rather than going through robocopy and then regex/split the output.

---

<div class="post-metadata">

**Author:** ![david-schmidtberger](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@david-schmidtberger](https://forums.powershell.org/u/david-schmidtberger)\
**Post date:** [May 3, 2018, 6:55am UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/13 "2018-05-03T06:55:19Z")

</div>

I use alphafs to get around this limitation.

> **[PowerShell · alphaleonis/AlphaFS Wiki](https://github.com/alphaleonis/AlphaFS/wiki/PowerShell)**
>
> AlphaFS is a .NET library providing more complete Win32 file system functionality to the .NET platform than the standard System.IO classes. - PowerShell · alphaleonis/AlphaFS Wiki

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [May 3, 2018, 7:20am UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/14 "2018-05-03T07:20:59Z")

</div>

Get-ChildItem2 uses AlphaFS, I’m pretty sure, and it doesn’t seem to handle UNC long paths. ☹

---

<div class="post-metadata">

**Author:** ![nlong12](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/nlong12/32/123_2.png) [@nlong12](https://forums.powershell.org/u/nlong12)\
**Post date:** [May 3, 2018, 10:38am UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/15 "2018-05-03T10:38:10Z")

</div>

Joel,  
Wanted to let you know the PSDrive -Persist Parameter allowed get-childitem2 to work with no directory length issues!

Many thanks for your input

Norm

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [May 3, 2018, 2:15pm UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/16 "2018-05-03T14:15:43Z")

</div>

Interesting. I might have to go submit an issue on the github for NTFSSecurity if I can find it – seems like it’s just an issue it not checking the PSDrives directly.

---

<div class="post-metadata">

**Author:** ![david-schmidtberger](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@david-schmidtberger](https://forums.powershell.org/u/david-schmidtberger)\
**Post date:** [May 3, 2018, 2:37pm UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/17 "2018-05-03T14:37:42Z")

</div>

hmm thats interesting Joel, i use alpha directly on an almost daily basis to delete users home drives, and enumerating/deleting always works on the long file-paths.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:35pm UTC](https://forums.powershell.org/t/get-childitem-the-specified-path-file-name-or-both-are-too-long-the-fully-q/10613/18 "2024-05-16T20:35:06Z")

</div>

Closed on May 16, 2024, 8:35pm UTC


