``new-item`` cannot create hardlinks with ``[]`` in the name on win10
See original GitHub issueWhen 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
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:12 (6 by maintainers)
Top GitHub Comments
How this is still an open issue in 2022?
New-Item -ItemType SymbolicLink
with-Target
contains[]
still does not work.Another way of putting it:
If we accepted the - questionable - premise that the
-Value
/-Target
argument should be interpreted as a wildcard pattern:[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.