2

I try to use long UNC paths with Get-ChildItem in Powershell like

Get-ChildItem -Path "\\?\c:\blabla"

and Powershell says that there are illegal characters in the path. The very same path works with Resolve-Path. How can I use the "\\?\" syntax with gci?

Christoph
  • 1,964
  • 2
  • 27
  • 44
  • If you aren't doing any variable substitution, you can try either `-LiteralPath "yourpath"` or using single quotes `-Path 'PathWithSpecialChars'` – Eris Nov 04 '13 at 17:39
  • I do not use variables atm. However, I found that using the admin share C$ instead of C: does not produce the Syntax error, but `gci` does not have any output either. – Christoph Nov 04 '13 at 17:53
  • 2
    Additionally, `gci "\\?\UNC\c:\blabla"` does not yield the syntax error, but has no results, too. – Christoph Nov 04 '13 at 17:56

3 Answers3

1

I can't seem to find an incantation that works with ?. However the following do work:

gci '\\localhost\C$\'
gci ('\\{0}\C$\' -f $ENV:COMPUTERNAME)

Update: Reference: UNC path does not work with .NET?

The \\?\ portion is windows specific and essentially does the same thing as --% in powershell, which is to say 'Everything after this is a literal string'

Example:

$LongUncPath = '\\?\C:\'
Get-ChildItem -Path:$LongUncPath.TrimStart('\\?\')
Community
  • 1
  • 1
Eris
  • 7,378
  • 1
  • 30
  • 45
  • 1
    That is what I see, too. UNC works, but long UNC does not. The problem is that I need long UNC. Does anyone know whether long UNC is not working *by design*? I would need to search for a workaround then... – Christoph Nov 05 '13 at 08:48
1

Great news! PowerShell v6.0.0-beta.3 and up now supports UNC paths by default; it automatically prepends the UNC string to paths > 260 characters:

https://github.com/PowerShell/PowerShell/releases/tag/v6.0.0-beta.3 https://github.com/PowerShell/PowerShell/pull/3960 https://github.com/PowerShell/PowerShell/pull/3960/files/3e7e28d49f306ab4874e32cf10eabb43559dae26#diff-52b1a915619c71b288b3f92f944924c4

Fix PowerShell Core to allow the use of long paths that are more than 260 characters. (#3960)

...

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


You simply have to download that version or later (via. the PowerShell tags). I just downloaded v6.0.0-beta.9 for Windows x64 onto my Windows 10 machine and tested it out, creating a path with some 460 characters!

Andrew
  • 5,839
  • 1
  • 51
  • 72
0

I found that I had non-printable characters at the end of my path. I corrected "Illegal characters in path" by use of trim() to remove them.

$path.trim()

Kirt Carson
  • 756
  • 6
  • 3