Skip to content

Set-Location fails for paths with special characters when switching drives #25282

@genXdev

Description

@genXdev

Prerequisites

Steps to reproduce

Steps to Reproduce

Use two different, valid and accessible drive letters on your system, I use C: and E:

  1. Create a directory with special characters in its name:
    md "e:\This my test [Folder] with bracket characters"
  2. Navigate into the directory:
    cd 'e:\This my test `[Folder`] with bracket characters\'
  3. Switch to another drive:
    c:
  4. 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.
    
  5. 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… bracket
characters: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 characters
CategoryInfo          : ObjectNotFound: (E:\This my test [Fo… bracket characters:String)
[Set-Location], ItemNotFoundException
FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
InvocationInfo        :
    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    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :
      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

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-FileSystem-Providerspecific to the FileSystem providerIssue-BugIssue has been identified as a bug in the productKeepOpenThe bot will ignore these and not auto-closeUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdlets-Managementcmdlets in the Microsoft.PowerShell.Management moduleWG-ReviewedA Working Group has reviewed this and made a recommendation

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Reviewed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions