Skip to content

new-item cannot create hardlinks with [] in the name on win10 #14960

Closed

Description

@LDSpits

When attempting to create a hardlink with special characters in powershell 7.1, the command fails with the message
New-Item: Cannot find path 'D:\debugging\[name] with special chars.txt' because it does not exist.
for items with the special characters []

I have attempted to create a hardlink with the mklink /H command on the commandpromt, Which is able to accept targets with special characters.

Since the commandprompt is able to do this, I would expect to be able to do the same with powershell

Steps to reproduce

# Validating being able to create normal hardlinks
New-Item -ItemType HardLink -Target (Resolve-Path .\test.txt) -Path "./test-hard.txt"
New-Item -ItemType HardLink -Target (Resolve-Path '.\test name.txt') -Path "./test-hard spaced.txt"

# attempting to create a hardlink with `[]` in the name
New-Item -ItemType HardLink -Target (Resolve-Path '.\`[name`] with special chars.txt') -Path "./test-hard spaced 2.txt"

Expected behavior

Powershell creates a hardlink named test-hard spaced 2.txt in the current directory.

    Directory: D:\debugging

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---            7-3-2021    15:21              0 test-hard spaced 2.txt

Actual behavior

The Cmdlet fails with the following message

New-Item: Cannot find path 'D:\debugging\[name] with special chars.txt' because it does not exist.

I have validated that the target file actually exists in the file explorer, and the command prompt is able to handle this case just fine with the mklink tool. Provided you escape the name correctly.

D:\debugging>mklink /H "test-hard spaced 2.txt" "[name] with special chars.txt"
Hardlink created for test-hard spaced 2.txt <<===>> [name] with special chars.txt

Environment data

PSVersion                      7.1.2
PSEdition                      Core
GitCommitId                    7.1.2
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. p0W3RH311 commented on Mar 7, 2021

    @p0W3RH311

    try to execute Resolve-Path with -LiteralPath

  2. LDSpits commented on Mar 7, 2021

    @LDSpits
    ContributorAuthor

    try to execute Resolve-Path with -LiteralPath

    This still gives me a failure

    New-Item -ItemType HardLink -Target (Resolve-Path -LiteralPath '.\[name] with special chars.txt') -Path "./test-hard spaced 2.txt"
    New-Item: Cannot find path 'D:\debugging\[name] with special chars.txt' because it does not exist.
  3. mklement0 commented on Mar 7, 2021

    @mklement0
    Contributor

    Please see #14534

  4. 237dmitry commented on Mar 7, 2021

    @237dmitry

    It works for me:
    hl

  5. mklement0 commented on Mar 7, 2021

    @mklement0
    Contributor

    237dmitry, this is about the -Target parameter, not the -Name parameter.

  6. 237dmitry commented on Mar 8, 2021

    @237dmitry

    this is about the -Target parameter, not the -Name parameter.

    I'm sorry, I'm always inattentive. mklink is of course easier.

    $item = Get-Item '.\`[test`] test `[1`].txt'
    New-Item -ItemType HardLink -Name file.txt -Target $($item -replace '(?=[\[\]])','``')
    

    Double `` as escapes

    New-Item -ItemType HardLink -Name file.txt -Target 'C:\Path\To\file ``[123``].txt'
    
  7. mklement0 commented on Mar 8, 2021

    @mklement0
    Contributor

    237dmitry, while it's good to know about workarounds, this is ultimately about fixing PowerShell's behavior, as discussed in #14534.

    Specifically:

    • mklink is not only Windows-specific, it is a command internal to cmd.exe.

    • While doubly escaping the [ and ] characters with two ` characters happens to work, that in itself is a bug - see Backtick escaping inconsistent #7999

  8. mklement0 commented on Mar 8, 2021

    @mklement0
    Contributor

    Another way of putting it:

    • If we accepted the - questionable - premise that the -Value / -Target argument should be interpreted as a wildcard pattern:

      • Then [WildcardPattern]::Escape('C:\Path\To\file [123].txt') - i.e. 'C:\Path\To\file `[123`].txt' - should work: one verbatim ` to escape each metacharacter.
    • We shouldn't accept this premise, however, because that argument should be interpreted as a literal (verbatim) path.

  9. added
    WG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.
    on Mar 8, 2021
  10. abaumgaertner commented on Apr 5, 2021

    @abaumgaertner

    similar issue #6232

  11. CCRcmcpe commented on Nov 15, 2022

    @CCRcmcpe

    How this is still an open issue in 2022? New-Item -ItemType SymbolicLink with -Target contains [] still does not work.

  12. daxian-dbw commented on Jun 12, 2023

    @daxian-dbw
    Member

    The Engine WG reviewed this issue and believe this is a duplicate of #14534. Will close this issue as a duplicate.

  13. added and removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Jun 12, 2023
  14. ghost closed this as completedon Jun 13, 2023
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

    Resolution-DuplicateThe issue is a duplicate.WG-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