question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
CCRcmcpecommented, Nov 15, 2022

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

1reaction
mklement0commented, Mar 8, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I create file hardlink in PowerShell on Windows 10?
1 Answer 1 · 16. For directories you can use New-Item -ItemType Junction -Name System33 -Value c:\windows\system32 · 1. Another way to do ......
Read more >
Create Symbolic Link in Windows 10 with PowerShell
This post explains how to create symbolic links, hard links and directory junctions in Windows 10 with PowerShell cmdlets.
Read more >
How do you create a new symlink in windows 10 using ...
Start powershell as admin; You need to know 1) the path to target of the link 2) path to location where you want...
Read more >
Create Soft and Hard Symbolic Links in Windows
hi, got a folder A in which I succesfully created a junction (established this by ntfslinkview) to another folder B using powershell ...
Read more >
Creating Symbolic Links (Symlinks) in Windows
To create symbolic or hard links in Windows, you can use the built-in mklink tool or PowerShell. mklink command on Windows. mklink has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found