PowerShell
PowerShell copied to clipboard
On Windows, FileSystemProvider's implementation of ItemExists does not work with long paths.
On Windows, the overridden method ItemExists in FileSystemProvider does not properly handle paths with lengths in excess of MAX_PATH (260) characters. This adversely affects several cmdlets, including Test-Path, Set-Location, and Remove-Item, as well as TAB expansion.
Steps to reproduce
PS C:\Temp\longish-directory-name> $longname = 'a' * 250
PS C:\Temp\longish-directory-name> New-Item -ItemType Directory $longname
Directory: C:\temp\longish-directory-name
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 5/30/2017 8:59 PM aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaa
PS C:\Temp\longish-directory-name> Test-Path $longname
False
PS C:\Temp\longish-directory-name> Set-Location aa<TAB>
PS C:\Temp\longish-directory-name> Set-Location $longname
Set-Location : Cannot find path 'C:\temp\longish-directory-name\aaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
because it does not exist.
At line:1 char:1
+ Set-Location $longname
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\temp\longish...aaaaaaaaaaaaa
aa:String) [Set-Location], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLo
cationCommand
PS C:\Temp\longish-directory-name> Remove-Item $longname
Remove-Item : Cannot find path 'C:\temp\longish-directory-name\aaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
because it does not exist.
At line:1 char:1
+ Remove-Item $longname
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\temp\longish...aaaaaaaaaaaaa
aa:String) [Remove-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.Remov
eItemCommand
Expected behavior
The long-named sub-directory was successfully created, so
Test-Path $longnameshould return TrueSet-Location aa<TAB>should expand to the long directory nameSet-Location $longnameshould change to the long-named sub-directory- After returning to the parent directory,
Remove-Item $longnameshould delete the directory
All of these work correctly on Unix environments.
Actual behavior
Test-Path $longnamereturns FalseSet-Location aa<TAB>does nothingSet-Location $longnamefails with cannot-find-path errorRemove-Item $longnamefails with cannot-find-path error
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 6.0.0-beta
PSEdition Core
BuildVersion 3.0.0.0
CLRVersion
GitCommitId v6.0.0-beta.1-32-g5cc46d9bcd9627f4a62b3c4825d...
OS Microsoft Windows 10.0.14393
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0