You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Set-Location fails for paths with special characters when switching drives #25282
Use two different, valid and accessible drive letters on your system, I use C: and E:
Create a directory with special characters in its name:
md "e:\This my test [Folder] with bracket characters"
Navigate into the directory:
cd 'e:\This my test `[Folder`] with bracket characters\'
Switch to another drive:
c:
Attempt to switch back to the original directory:
e:
Observed Result: Error:
Set-Location: Cannot find path 'E:\This my test [Folder] with bracket characters' because it does not exist.
Use -LiteralPath when switching back to the original directory:
cd -LiteralPath e:
Observed Result:
Successful navigation to the directory:
PS E:\This my test [Folder] with bracket characters>
Expected behavior
PS C:\> e:PS E:\This my test [Folder] with bracket characters>
Actual behavior
PS C:\> e:Set-Location: Cannot find path 'E:\This my test [Folder] with bracket characters' because it does not exist.PS C:\>
Error details
Exception : Type : System.Management.Automation.ItemNotFoundException ErrorRecord : Exception : Type : System.Management.Automation.ParentContainsErrorRecordException Message : Cannot find path 'E:\This my test [Folder] with bracket characters'because it does not exist. HResult : -2146233087 TargetObject : E:\This my test [Folder] with bracket characters CategoryInfo : ObjectNotFound: (E:\This my test [Fo… bracketcharacters:String) [], ParentContainsErrorRecordException FullyQualifiedErrorId : PathNotFound ItemName : E:\This my test [Folder] with bracket characters SessionStateCategory : Drive TargetSite : Name : SetLocation DeclaringType : [System.Management.Automation.SessionStateInternal] MemberType : Method Module : System.Management.Automation.dll Message : Cannot find path 'E:\This my test [Folder] with bracket characters'because it does not exist. Source : System.Management.Automation HResult : -2146233087 StackTrace : at System.Management.Automation.SessionStateInternal.SetLocation(String path,CmdletProviderContext context, Boolean literalPath) at Microsoft.PowerShell.Commands.SetLocationCommand.ProcessRecord()TargetObject : E:\This my test [Folder] with bracket charactersCategoryInfo : ObjectNotFound: (E:\This my test [Fo… bracket characters:String)[Set-Location], ItemNotFoundExceptionFullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommandInvocationInfo : MyCommand : Set-Location ScriptLineNumber : 1 OffsetInLine : 1 HistoryId : 91 Line : Set-Location $MyInvocation.MyCommand.Name Statement : Set-Location $MyInvocation.MyCommand.Name PositionMessage : At line:1 char:1 + Set-Location $MyInvocation.MyCommand.Name + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ InvocationName : Set-Location CommandOrigin : InternalScriptStackTrace : at <ScriptBlock>, <No file>: line 1 at <ScriptBlock>, <No file>: line 1PipelineIterationInfo : 0 1
Environment data
Name Value
---------
PSVersion 7.5.0
PSEdition Core
GitCommitId 7.5.0
OS Microsoft Windows 10.0.26100
Platform Win32NT
PSCompatibleVersions {1.0,2.0,3.0,4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Additional Observation: Resolve-Path cmdlet with -RelativeBasePath parameter
I have discovered a similar issue affecting the Resolve-Path cmdlet when using the -RelativeBasePath parameter along with -Relative for paths containing special characters like [ and ].
Steps to Reproduce:
Create a directory with special characters in its name:
PS C:\>md "E:\This my test [Folder] with bracket characters"
Create a test file in the directory:
PS C:\>New-Item-Path 'E:\This my test [Folder] with bracket characters\file-1.txt'-ItemType File
Navigate to the directory:
PS C:\>cd 'E:\This my test `[Folder`] with bracket characters\'
Resolve the full path of the test file:
PS E:\This my test [Folder] with bracket characters>Resolve-Path-LiteralPath 'E:\This my test [Folder] with bracket characters\file-1.txt'
Observed Result:
Path
----
E:\This my test [Folder] with bracket characters\file-1.txt
Attempt to resolve the relative path using -Relative and -RelativeBasePath:
PS E:\This my test [Folder] with bracket characters>Resolve-Path-LiteralPath 'E:\This my test [Folder] with bracket characters\file-1.txt'-Relative -RelativeBasePath .\
Observed Result:
Resolve-Path: Cannot find path 'E:\This my test [Folder] with bracket characters' because it does not exist.
Analysis:
This issue shows that the Resolve-Path cmdlet does not handle paths containing special characters like [ and ] correctly when using both -Relative and -RelativeBasePath. This behavior is similar to the previously reported issue with the Set-Location cmdlet. It indicates potential inconsistencies in how these parameters interpret or handle paths with special characters.
This function uses positional arguments so the default -Path is used, and the current path is treated like a wildcard path: the brackets in the path aren't taken literally but get resolved. The resulting path obviously does not exist.
A solution would be to use named arguments and change the function so that it explicitly uses -LiteralPath:
This would make a lot of sense since $MyInvocation.MyCommand.Name is always a literal path and never a wildcard expression (unless I am overlooking something).
This case also serves as a reminder that with all filesystem-related cmdlets, -LiteralPath is the better choice in the majority of cases, and the (default - sic!) -Path must only be used if you are really intending to resolve wildcards.
That said, in this case, while you could redefine the functions to fix the problem, they should be corrected by core.
However, I found that this change alone does not fix the issue. The drive-switching syntax (e.g., E:) does not go through these drive functions. Instead, it is processed directly in SessionStateLocationAPIs.SetLocation(), where CurrentDrive.CurrentLocation is combined with the drive name and then passed through wildcard expansion.
The actual code path is:
User types E:
SessionStateLocationAPIs.SetLocation() is called
At line 309-312, the path is constructed from CurrentDrive.CurrentLocation
This path is then passed to Globber.GetGlobbedMonadPathsFromMonadPath() which performs wildcard expansion
Brackets in the path are interpreted as wildcard patterns, causing the error
Therefore, the fix in my previous PR #26510 (modifying SessionStateLocationAPIs.cs to suppress wildcard expansion for paths restored from drive's CurrentLocation) appears to be necessary to resolve this issue.
Ilya (@iSazonov) Should I resubmit the approach from PR #26510, or is there an alternative direction WG would prefer?
I see the same approach in #25282. If this doesn't work I afraid there is more fundamental problem or problems we need to investigate. Perhaps root of the issue is that we extract an already processed path and re-process it.
Activity
genXdev commentedon Apr 5, 2025
Additional Observation:
Resolve-Pathcmdlet with-RelativeBasePathparameterI have discovered a similar issue affecting the
Resolve-Pathcmdlet when using the-RelativeBasePathparameter along with-Relativefor paths containing special characters like[and].Steps to Reproduce:
Create a directory with special characters in its name:
Create a test file in the directory:
Navigate to the directory:
Resolve the full path of the test file:
Observed Result:
Attempt to resolve the relative path using
-Relativeand-RelativeBasePath:Observed Result:
Analysis:
This issue shows that the
Resolve-Pathcmdlet does not handle paths containing special characters like[and]correctly when using both-Relativeand-RelativeBasePath. This behavior is similar to the previously reported issue with theSet-Locationcmdlet. It indicates potential inconsistencies in how these parameters interpret or handle paths with special characters.237dmitry commentedon Apr 5, 2025
What is the current path from where you are trying to resolve the file path? Is it part of this file system tree?
genXdev commentedon Apr 5, 2025
237dmitry
The current path from where to resolve the file path would be:
'E:\This my test [Folder] with bracket characters\file-1.txt'
I'll edit the reproduction steps to include the 'PS E:\This my test [Folder] with bracket characters>' prompt.
TobiasPSP commentedon Apr 16, 2025
When you switch to a different drive by entering the drive name, PS internally runs a function:
This function uses positional arguments so the default
-Pathis used, and the current path is treated like a wildcard path: the brackets in the path aren't taken literally but get resolved. The resulting path obviously does not exist.A solution would be to use named arguments and change the function so that it explicitly uses
-LiteralPath:This would make a lot of sense since
$MyInvocation.MyCommand.Nameis always a literal path and never a wildcard expression (unless I am overlooking something).This case also serves as a reminder that with all filesystem-related cmdlets,
-LiteralPathis the better choice in the majority of cases, and the (default - sic!)-Pathmust only be used if you are really intending to resolve wildcards.That said, in this case, while you could redefine the functions to fix the problem, they should be corrected by core.
TobiasPSP commentedon May 7, 2025
The cmdlet WG reviewed this. This is a bug, and
-LiteralPathshould be added toSet-Locationin the automatically defined drive-letter functions.iSazonov commentedon May 13, 2025
There already was such conclusion #12294 (comment)
iSazonov commentedon May 13, 2025
For reference #20701
Fix PowerShell#25282: Set-Location fails for paths with special chara…
yotsuda commentedon Nov 27, 2025
I attempted to implement the fix as WG proposed - modifying the
SetDriveScriptBlock(embedded functions likefunction:C:).Here is the diff I tried:
However, I found that this change alone does not fix the issue. The drive-switching syntax (e.g.,
E:) does not go through these drive functions. Instead, it is processed directly inSessionStateLocationAPIs.SetLocation(), whereCurrentDrive.CurrentLocationis combined with the drive name and then passed through wildcard expansion.The actual code path is:
E:SessionStateLocationAPIs.SetLocation()is calledCurrentDrive.CurrentLocationGlobber.GetGlobbedMonadPathsFromMonadPath()which performs wildcard expansionTherefore, the fix in my previous PR #26510 (modifying
SessionStateLocationAPIs.csto suppress wildcard expansion for paths restored from drive'sCurrentLocation) appears to be necessary to resolve this issue.Ilya (@iSazonov) Should I resubmit the approach from PR #26510, or is there an alternative direction WG would prefer?
iSazonov commentedon Nov 28, 2025
I see the same approach in #25282. If this doesn't work I afraid there is more fundamental problem or problems we need to investigate. Perhaps root of the issue is that we extract an already processed path and re-process it.