You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Creating symbolic links or junctions with New-Item doesn't work if target contains '[' or ']' #6232
Name Value
---- -----
PSVersion 6.0.1
PSEdition Core
GitCommitId v6.0.1
OS Microsoft Windows 6.1.7601 S
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Reacted by Michael Klement, anthonypants, Jinwook Jeong (Edgar), Jef, KaXaSA, Robert Martin, Rodrigo Machuca and Jacobin
To be clear: wildcard support should not apply when creating new items.
NewItem's -Path parameter:
is, on the one hand, commendably already treated as a literal - the new item is created with the specified name as-is.
Similarly, -Name is treated literally, but invariably creates the new item in the current location.
on the other hand, the parameter name clashes with the use of -Path in cmdlets such as Get-ChildItem, where it implies wildcard support, in contrast with the -LiteralPath parameter; New-Item doesn't even have a -LiteralPath parameter (arguably, though, it should be added as an alias of -Path).
Similarly, the -Value parameter - whose alias name is -Target - should always treat a value passed to it as a literal, but currently doesn't - and that is the bug.
kwkam: -Name is not an alias of -LiteralPath, not least because New-Item doesn't even have a -LiteralPath parameter (see above). Aside from that, the inappropriate treatment of a value passed to -Value / -Target as a wildcard expression is the same.
Michael Klement (@mklement0) For consistency with other cmdlet's -Path parameter, and for the convenient with tab completion, I think -Path should stay as is. As for the -Path and -Name, what if we want to do, for example, New-Item -Path '`[path`]/item_[0-9]' -Name '[sub]/dir' -ItemType Directory? -Name can act as -LiteralPath in my opinion.
I wasn't suggesting that New-Item's -Path be changed, I was suggesting that -LiteralPath be added as an alias for it.
The reason for suggesting that is that -Pathalready acts the way -LiteralPath usually does.
Conceptually speaking, -Name is a path component, and, more precisely, the leaf component - it should never accept a path.
Unfortunately, it currently does accept a path, namely a relative one: either relative to the path specified in -Path, or, in the latter's absence, relative to the current directory.
Aside from that, -Name doesn't - and shouldn't - accept wildcard patterns either.
For consistency, given that -Path and -Name don't accept wildcard patterns, neither should -Value (a.k.a. -Target).
Similarly, the -Value parameter - whose alias name is -Target - should always treat a value passed to it as a literal, but currently doesn't - and that is the bug.
Hey! Googling an Error lead me here. Trying to create a Symlink to a path that contains square brackets using
Running 6.2.2 on Linux and found this issue after hours of scratching my head. In my use case in particular it's important the original file names are preserved.
I tried escaping wildcard characters in the value I pass to -Value/-Target, but that just results in ItemNotFoundException. Re-reading the above, I've found that double-escaping the path first does solve the problem, but that's obviously not something that should be expected of anyone.
What's confusing me the most is from what I read I still don't quite understand why -Value isn't presumable literal. The discussion on the above PR seems much more concerned about -Path and doesn't seem to at all address that -Value should always be literal regardless of how -Path is handled.
Sam (@sam-6174), yes, that's a viable workaround (previously mentioned); note that none of the double quotes are needed in your sample call. Also, since your example creates a hard link (/h) let me show symlink examples (cmd /c mklink /? shows help):
# Symlink to a FILE
cmd /c mklink $link_file_path$src_file_path# Symlink to a DIRECTORY
cmd /c mklink /D $link_file_path$src_file_path
Sam (@sam-6174), yes, that's a viable workaround (previously mentioned); note that none of the double quotes are needed in your sample call. Also, since your example creates a hard link (/h) let me show symlink examples (cmd /c mklink /? shows help):
# Symlink to a FILE
cmd /c mklink $link_file_path$src_file_path# Symlink to a DIRECTORY
cmd /c mklink /D $link_file_path$src_file_path
As it turns out, there's actually not enough quotes in the original code, i.e. this...
Activity
iSazonov commentedon Feb 24, 2018
It is expected behavior because we support wildcards in paths.
Perhaps we could add
-LiteralPathinNew-Itemlkydev commentedon Feb 25, 2018
I had the problem with the '-Value' parameter, not '-Path' or even '-LiteralPath'.
So perhaps we need '-LiteralValue', not '-LiteralPath'.
kwkam commentedon Feb 26, 2018
-Nameis-LiteralPathThis should work:
mklement0 commentedon Feb 26, 2018
Ilya (@iSazonov):
To be clear: wildcard support should not apply when creating new items.
NewItem's-Pathparameter:is, on the one hand, commendably already treated as a literal - the new item is created with the specified name as-is.
-Nameis treated literally, but invariably creates the new item in the current location.on the other hand, the parameter name clashes with the use of
-Pathin cmdlets such asGet-ChildItem, where it implies wildcard support, in contrast with the-LiteralPathparameter;New-Itemdoesn't even have a-LiteralPathparameter (arguably, though, it should be added as an alias of-Path).Similarly, the
-Valueparameter - whose alias name is-Target- should always treat a value passed to it as a literal, but currently doesn't - and that is the bug.kwkam:
-Nameis not an alias of-LiteralPath, not least becauseNew-Itemdoesn't even have a-LiteralPathparameter (see above). Aside from that, the inappropriate treatment of a value passed to-Value/-Targetas a wildcard expression is the same.kwkam commentedon Feb 26, 2018
Okay I forgot I was using a patched(#5896) build.
Michael Klement (@mklement0) For consistency with other cmdlet's
-Pathparameter, and for the convenient with tab completion, I think-Pathshould stay as is. As for the-Pathand-Name, what if we want to do, for example,New-Item -Path '`[path`]/item_[0-9]' -Name '[sub]/dir' -ItemType Directory?-Namecan act as-LiteralPathin my opinion.mklement0 commentedon Feb 26, 2018
kwkam:
I wasn't suggesting that
New-Item's-Pathbe changed, I was suggesting that-LiteralPathbe added as an alias for it.The reason for suggesting that is that
-Pathalready acts the way-LiteralPathusually does.Conceptually speaking,
-Nameis a path component, and, more precisely, the leaf component - it should never accept a path.Unfortunately, it currently does accept a path, namely a relative one: either relative to the path specified in
-Path, or, in the latter's absence, relative to the current directory.Aside from that,
-Namedoesn't - and shouldn't - accept wildcard patterns either.For consistency, given that
-Pathand-Namedon't accept wildcard patterns, neither should-Value(a.k.a.-Target).Moonsilence commentedon Jan 2, 2019
Hey! Googling an Error lead me here. Trying to create a Symlink to a path that contains square brackets using
New-Item -Path C:\my\path\cover.jpg -ItemType SymbolicLink -Value C:\my\path[1995]\cover.jpg
"not found"
Has this bug been fixed yet?
vexx32 commentedon Jan 2, 2019
I see a lot of PRs with kwkam's work but I'm not sure that specific issue has been completely fixed yet.
anthonypants commentedon Apr 14, 2019
This is definitely still happening in 6.2.0.
JessiAuro commentedon Aug 6, 2019
Running 6.2.2 on Linux and found this issue after hours of scratching my head. In my use case in particular it's important the original file names are preserved.
I tried escaping wildcard characters in the value I pass to
-Value/-Target, but that just results inItemNotFoundException. Re-reading the above, I've found that double-escaping the path first does solve the problem, but that's obviously not something that should be expected of anyone.What's confusing me the most is from what I read I still don't quite understand why
-Valueisn't presumable literal. The discussion on the above PR seems much more concerned about-Pathand doesn't seem to at all address that-Valueshould always be literal regardless of how-Pathis handled.KaXaSA commentedon Sep 21, 2020
13 remaining items
mklement0 commentedon Jun 5, 2023
Sam (@sam-6174), yes, that's a viable workaround (previously mentioned); note that none of the double quotes are needed in your sample call. Also, since your example creates a hard link (
/h) let me show symlink examples (cmd /c mklink /?shows help):soredake commentedon Dec 5, 2023
Still relevant.
sam-6174 commentedon Dec 6, 2023
As it turns out, there's actually not enough quotes in the original code, i.e. this...
... needs to be updated to this...
Without these quotes, the original code will fail if
$link_file_pathcontains an ampersand, i.e.&SteveL-MSFT commentedon Dec 20, 2023
In #13136 the PS-Committee made the decision that
-Targetshould always be treated as literalGameMicrowave commentedon Jan 5, 2025
Yes, I met the error today, the -target should treat the path as literal always😡
microsoft-github-policy-service commentedon May 12, 2025
📣 Hey @lkydev, how did we do? We would love to hear your feedback with the link below! 🗣️
🔗 https://aka.ms/PSRepoFeedback