I am trying to return a list of files from a share with specific properties. The problem is with the character limitation for the path. I have looked at many articles and tried several things to get this to work but to no avail.

Recently I have updated powershell on the system that I need to run this on with 5.1. As I understand, the following snipit should work however I am getting an invalid character error when I try to run it. Any help appreciated.

Thank you

Get-ChildItem -LiteralPath \\?\UNC\Server\share$ -File -Recurse -Attributes sparse+offline | select fullname | Export-Csv D:\temp\DeptIT.csv -NoTypeInformation
3 Spice ups

Please post the full error that you are getting.

Get-ChildItem : Illegal characters in path.
At line:1 char:1

  • Get-ChildItem -LiteralPath \?\UNC\server\share$\IT -File -Recurse …
  • CategoryInfo : NotSpecified: (:slight_smile: [Get-ChildItem], ArgumentException
  • FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.GetChildItemCommand

Is the question mark actually in the path? Do you get an error with this?

Get-ChildItem -Path \\Server\share$ -File -Recurse -Attributes sparse+offline | select fullname | Export-Csv D:\temp\DeptIT.csv -NoTypeInformation

Am just going to ask the question @techadmin8 ​ is trying to avoid in a round about polite way. .

Is the part edited for the post here " \?\UNC\server\share$\IT" or this actually in the script ?

:slight_smile:

Yes that is edited for the post. :wink:

Yes the ? is in the command. That is apparently part of the command for long paths. If you are looking to get information locally say, the command is \?\C:\whatever. At least that is what I am getting from articles online. That should bypass the 246 character limit when used with -LiteralPath.

I do not get an error running without \?\UNC but I get long path errors trying to retrieve files.

you can try to use robocopy to list the filenames.

I was recently working on a similar problem; have users having trouble editing data because their paths are like short books.

Did you not have quotes around your path?

So, to find those PATH TOO LONG attributes:

$root = '\\UNC\?\Server\Share'
$folders = gci -LiteralPath $root -directory -recurse -force -ea SilentlyContinue -ev ERR | where {whatever you need} | select -exp FullName

You likely don’t need the where clause; I did.
Notice the SINGLE QUOTES around the path to the value for $root.
I did a bunch of stuff with folders which isn’t relevant to your issue.
However:

if($err) {
    foreach ($e in $err) {
        $fName = $e.TargetObject
        $eExc  = $e.Exception
        if($eExc -match "Could not find part of the path") {
            $len = $fName.Length
            write-host "PATH TOO LONG for $fname with $len"
        }
    }
}

Even better, write this out to a log and send it to the folks creating their own nightmares.
YW!

Sorry for the delay in replying.

Though I am sure that any of these would work for me I have found that just by making sure Long Paths was enabled in the registry on a Server 2019 box I am able to get the information I need as this is supported natively in that OS.

Thanks for all the replies!