Skip to content

New-Item -Target parameter mishandles paths that look like wildcard patterns when creating symbolic links / reparse points #14534

Closed

Description

Note: I'm writing this bug report from the perspective of the green-lit proposal to interpret -Target arguments as literal (verbatim) paths - see #13136. Perhaps these two issues can be tackled together.

By contrast, in Windows PowerShell you must escape paths that look like wildcard patterns, i.e. paths that contain [ and ].
Using this escaping as of PowerShell Core in 7.2.0-preview.2 doesn't work either, however, because the escaped path, while recognized as an existing item, is them mistakenly used verbatim as the link's target, resulting in a broken link.

Steps to reproduce

Push-Location ($tempDir = New-Item -Type Directory -Force "temp:/$PID")

try
{

  # Use a file name with '[' and ']' - without that, the test would succeed.
  $target = Join-Path $pwd.ProviderPath '[1] file.txt'

  # Create the file.
  $null = New-Item -Path $target -Value hi -Force

  # Try to create a symlink named 'lnk' in subdir. 'sub' (to be created on demand
  # with -Force) pointing to the newly created file.
  # Note:
  #  If ([WildcardPattern]::Escape($target)) is used to escape the path,
  #  the command *seemingly* succeeds, but the *escaped* path is mistakenly
  #  used *verbatim* as the target, quietly resulting in a *broken link*.
  { New-Item -ErrorAction Stop -Path sub/lnk -Type SymbolicLink -Force -Target $target } |
    Should -Not -Throw

} finally {

  Pop-Location
  Remove-Item -ErrorAction Ignore -LiteralPath $tempDir -Recurse -Force

}

Expected behavior

The test should succeed.

Actual behavior

The test fails, because (by design unescaped) $target isn't recognized as an existing item, even though the error message correctly lists the path:

...
Cannot find path '.../[1] file.txt' because it does not exist.
...

Environment data

PowerShell Core 7.2.0-preview.2

Activity

  1. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Jan 3, 2021
  2. added
    Issue-Enhancementthe issue is more of a feature request than a bug
    WG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.
    and removed
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Jan 7, 2021
  3. abaumgaertner commented on Apr 5, 2021

    @abaumgaertner

    workaround: New-Item -Type Directory -Target [WildcardPattern]::Escape('abcd [bla]')
    similar issues: #6232

  4. mklement0 commented on Apr 5, 2021

    @mklement0
    ContributorAuthor

    Thanks, abaumgaertner (@abaumgaertner), you probably meant
    New-Item -Type SymbolicLink foo -Target ([WildcardPattern]::Escape('abcd [bla]')) (wrong type, missing ((...)), but, as stated in the initial post, this doesn't actually work:

    • The command seemingly succeeds,
    • but the resulting symlink is broken, because the escaped name is used verbatim as the target path.
  5. mklement0 commented on Apr 6, 2021

    @mklement0
    ContributorAuthor

    @OctaneTwisted found that escaping twice(!) is an effective workaround: #6232 (comment)

  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  7. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  8. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  9. Hashbrown777 commented on Nov 18, 2023

    @Hashbrown777

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

    I feel like biannually creating duplicate issues is not a productive way to see if issues still exist.

  10. microsoft-github-policy-service commented on Nov 25, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

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-Enhancementthe issue is more of a feature request than a bugResolution-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