0

I have the below line in my powershell script in windows 7

$subFolderItems = Get-ChildItem $i.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum

The problem I get this error The specified path, file name, or both are too long in some of the files.

I looked into it and found some suggestions to add \\?\ before the file path I tried as below and it's not working any advice?

$Base = '\\?\'
$subFolderItems = Get-ChildItem $Base$i.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
12
  • There is a 260 character limit for NTFS Paths. This is a Microsoft limitation. However, MS announced not too long ago that you can eliminate this limitation by editing the registry key LongPathsEnabled or enabling in group policy Commented Sep 19, 2019 at 13:00
  • what OS are you running one? win10 has a config option to enable long fie name support. Commented Sep 19, 2019 at 13:02
  • 1
    $Base$i.FullName --> ($Base + $i.FullName) Commented Sep 19, 2019 at 13:04
  • @AdminOfThings nice catch Commented Sep 19, 2019 at 13:04
  • @AdminOfThings I tried this but I get this error ` Get-ChildItem : Cannot retrieve the dynamic parameters for the cmdlet. Cannot process argument because the value of argument "path" is not valid. Change the value of the "path" argument and run the operation again.` Commented Sep 19, 2019 at 13:09

1 Answer 1

1

As you have commented, the path in $i.FullName is in UNC format (\\server\share\restofpath).

In that case, the prefix for long path should be \\?\UNC\ and the first two backslashes of the path itself need to be removed. In your case, it should be:
\\?\UNC\rackstation.mydom.com\main\sub.arch\mydom-customers\John_Marcus-123456

Using this prefix only works with the -LiteralPath parameter of Get-ChildItem.

Try

Get-ChildItem -LiteralPath ('\\?\UNC\' + $i.FullName.Substring(2))

For things like this, I always keep a small helper function handy:

function Add-LongPathPrefix([string] $Path) {
    if ([string]::IsNullOrWhiteSpace($Path) -or $Path.StartsWith('\\?\')) {   #'# nothing to do here
        return $Path
    }
    if ($Path.StartsWith('\\')) {
        # it's a UNC path like \\server\share\restofpath
        return '\\?\UNC\' + $Path.Substring(2)   #'#  --> \\?\UNC\server\share\restofpath
    }
    else {
        # it's a local path like X:\restofpath
        return '\\?\' + $Path                    #'#  --> \\?\X:\restofpath
    }
}

Use it like this:

Get-ChildItem -LiteralPath (Add-LongPathPrefix $i.FullName)

P.S. For this you need to have Powershell 5.1 or higher

Hope that helps

Sign up to request clarification or add additional context in comments.

7 Comments

I tried both suggestions and in both I get this error " Get-ChildItem : Illegal characters in path." But when I remove the "?" it gives the other error again.
@Tak I think you are NOT specifying the -LiteralPath parameter. You cannot omit that. What version of PowerShell are you using?
And how to specify -LiteralPath?
@Tak just exactly as i wrote it: Get-ChildItem -LiteralPath (Add-LongPathPrefix $i.FullName). If you simply do Get-ChildItem (Add-LongPathPrefix $i.FullName), the cmdlet tries to use the path as the first positional parameter called -Path. Please look up the parameters and see the difference between -Path and -LiteralPath here
I tried it but I still get this error Get-ChildItem : Illegal characters in path.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.