Skip to content

On Unix, PowerShell will not find files with backslashes in their names #3666

Closed

Description

@jeffbi

This is similar to #3414, but involves only the backslash portion.

The basis of the problem is premature normalization of paths. Path normalization happens quite early (and very frequently and in a great number of places in the PowerShell code) while processing a command. Usually this happens before the path reaches its provider, and often that normalization is a brute-force change one slash to another mechanism. Thus a path such as 1\2 on Unix is "normalized" to 1/2, ensuring that the path will never be found.

Using -LiteralPath does not prevent the path normalization, so that is not a workaround.

Steps to reproduce

From bash:

$ touch 'a\b'

then from PowerShell:

PS> Remove-Item a\b

or

PS> Move-Item a\b a-b

Expected behavior

The file a\b is deleted

Actual behavior

Remove-Item : Cannot find path 'a/b' because it does not exist.
At line:1 char:1
+ Remove-Item a\b
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (a/b:String) 
    [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

Environment data

> $PSVersionTable
Name                           Value                                                     
----                           -----                                                     
BuildVersion                   3.0.0.0                                                   
CLRVersion                                                                               
GitCommitId                    v6.0.0-alpha.18-38-g08e855556cff42274a5903799b826e78d0c...
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                   
PSEdition                      Core                                                      
PSRemotingProtocolVersion      2.3                                                       
PSVersion                      6.0.0-alpha                                               
SerializationVersion           1.1.0.1                                                   
WSManStackVersion              3.0                                                       

Activity

  1. added this to the milestone on May 1, 2017
  2. iSazonov commented on May 1, 2017

    @iSazonov
    Collaborator

    On Windows the same:

    PS C:\temp> New-Item "1/2"
    New-Item : Could not find a part of the path 'C:\temp\1\2'.
    At line:1 char:1
    + New-Item "1/2"
    + ~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (C:\temp\1\2:String) [New-Item], DirectoryNotFoundException
        + FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand
  3. jeffbi commented on May 1, 2017

    @jeffbi
    Author

    Ilya (@iSazonov) I think this is a separate issue. It may turn out to be related to path normalization, but I still think it should have it's own issue.

    This one is about attempting to rename, remove or move an existing file. The error message in this case is Cannot find path '1\2' because it does not exist. rather than Could not find a part of the path 'C:\temp\1\2'.

    I think you'd be unlikely to run into this in Windows, at least on its native file systems, since both forward and backslashes are disallowed. In Unix forward slashes are allowed, and it's simple create, from bash or other programs, a file with forward slashed in the name.

  4. iSazonov commented on May 2, 2017

    @iSazonov
    Collaborator

    It seems this should apply to the type of the file system and not to a platform.

  5. iSazonov commented on May 2, 2017

    @iSazonov
    Collaborator

    Interesting, Is [System.IO.Path]::GetInvalidPathChars() and [System.IO.Path]::GetInvalidFileNameChars() based on a platform or a file system type?

  6. jeffbi commented on May 2, 2017

    @jeffbi
    Author

    Ilya (@iSazonov) I would love to see this based on file system type rather than platform, along with a number of other areas. One problem is that I haven't found a way to definitively identify an NTFS file system under Linux.

    Regarding [System.IO.Path]::GetInvalidPathChars() and [System.IO.Path]::GetInvalidFileNameChars(), there appear to be separate implementations for each platform in coreclr.

    It seems that the notion of illegal characters is largely platform-based rather than file system based. As an experiment, I created a file named a>b<c:?| on an NTFS volume on my Linux box with the touch command. There are five characters in there that Windows considers to be illegal, but the NTFS file system accepts them and Windows displays the file properly in Windows Explorer. I can't do anything with it on Windows, such open or delete it, but it is recognized.

  7. iSazonov commented on May 2, 2017

    @iSazonov
    Collaborator

    This is specificity of higher-level API (Win2k).
    If so we must always follow the same rule and be platform based.We will not be able to be more flexible 😕

  8. modified the milestones: , on May 15, 2017
  9. 27 remaining items

  10. Serentty commented on May 17, 2020

    @Serentty

    I ran into this today when I accidentally created two directories with mkdir (the Linux version) instead of one with a space in its name, since I was trying to use the backslash to escape the space as I'm used to (I'm aware that PowerShell uses a different escape character). Afterwards, I was unable to delete the one whose name ended in a backslash.

  11. NicolaiLolansen commented on Oct 23, 2020

    @NicolaiLolansen

    When trying to run files with bash in powershell, the tab-completion resorts to doing bash .\script.sh which then won't find the file. Running bash script.sh or bash ./script.sh works fine, but the tab-completion uses .\ which does not work.

  12. Kuinox commented on Mar 7, 2024

    @Kuinox

    Hey Steve Lee (@SteveL-MSFT), your bot auto-closed this issue, the bug still exists.

  13. somescout commented on Jun 23, 2026

    @somescout

    The issue still exists and is still annoying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-FileSystem-Providerspecific to the FileSystem providerIssue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdletsgeneral cmdlet issuesWG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions