Get-ChildItem not working with long UNC path

有些话、适合烂在心里 提交于 2019-12-06 11:39:33

问题


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?


回答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!




回答2:


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('\\?\')



回答3:


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()



来源:https://stackoverflow.com/questions/19773157/get-childitem-not-working-with-long-unc-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!