I'm using New-Item cmdlet to create a new folder and surprisingly find that it has no -Literalpath parameter available. My path contains square brackets in it. What can I do to address this problem?
2 Answers
Yes, it is surprising that New-Item has no -LiteralPath parameter, especially given that:
its
-Pathparameter behaves like a-LiteralPathparameter, i.e. it treats the path as a literal (verbatim) one.- As for a potential future enhancement: While renaming the parameter isn't an option so as not to break backward compatibility, conceivably
-LiteralPathcould be introduced as an alias of-Path.
- As for a potential future enhancement: While renaming the parameter isn't an option so as not to break backward compatibility, conceivably
except if a
-Nameargument is also supplied, in which case the-Pathargument is unexpectedly treated as a wildcard expression, which notably causes problems with file paths that contain[This behavior should be considered a bug - see GitHub issue #17106
- A related bug is that the path passed to the
-Target(aka-Value) parameter too is interpreted as a wildcard expression - see this answer and GitHub issue #14534.
- A related bug is that the path passed to the
The workaround is to not use
-Nameand instead join the name component to the-Pathargument, such as withJoin-Path
# OK - with only a -Path argument, the path is taken *literally*
# Creates a subdir. literally named '[test]' with a file 'test.txt' in it.
New-Item -Force -Path '[test]\test.txt'
# !! BROKEN - with -Name also present, the -Path argument is
# !! interpreted as a *wildcard expression*
New-Item -Force -Path '[test]' -Name 'test.txt'
# WORKAROUND - use -Path only.
New-Item -Force -Path (Join-Path '[test]' 'test.txt')
3 Comments
name is unrelated to the value eg. New-Item -Type SymbolicLink -Name 'myShortcut' -Value '../to[another].file' (as mentioned in your #17106)-Value parameter inappropriately being interpreted as a wildcard expression as well, that is the subject of GitHub issue #13136. Fixing that has actually been green-lit, but no one has stepped up to implement it.-Value literally is to escape the wildcard metacharacters twice(!): New-Item -Type SymbolicLink -Name 'myShortcut' -Value '../to``[another``].file' - see GitHub issue #14534So, it was a little bit confusing what was the actual problem. So, you need to escape the brackets, the same way you would escape "\n" in strings - with " ` ". This will create the folder:
> New-Item -Path 'C:\stuff\powershell\`[test`]' -Name "221" -ItemType "directory"
But this will "silently fail":
> New-Item -Path 'C:\stuff\powershell\[test]' -Name "221" -ItemType "directory"
4 Comments
New-Item cmdlet is in a script with random paths feeding to it. So the escape way won't work.>cd [test] or >cd '[test]' ?-Name is also specified (specifying the whole path in -Path alone works as-is) and (b) it should never be necessary, because the behavior should be considered a bug - see GitHub issue #17106
> New-Item "this is [some] path" -ItemType "directory"this seems to be working for me. It works as well with full path. Could it be you are trying this without brackets ? Brackets are not in the Forbidden characters and namesNew-Item -Path "/the/path/to/your[file]here.txt" -ItemType Directory-Path. Say this codeNew-Item -Path "G:\1\jj[jj]j" -Name "121" -ItemType DirectoryNew-Item -Path "G:\1\jj[jj]j\jj[dqiw]j" -Type Directory. Problem solved.