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?

- 373
- 1
- 5
- 15
-
`> 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 names](https://learn.microsoft.com/en-us/windows/desktop/fileio/naming-a-file) – Bakudan Jan 15 '19 at 11:20
-
You can have square brackets in the item name....: `New-Item -Path "/the/path/to/your[file]here.txt" -ItemType Directory` – Stuart Jan 15 '19 at 11:22
-
@Bakudan @Stuart Yes, I can use brackets in the name of a newly created file/directory but not `-Path`. Say this code `New-Item -Path "G:\1\jj[jj]j" -Name "121" -ItemType Directory` – preachers Jan 15 '19 at 11:32
-
3@preachers `New-Item -Path "G:\1\jj[jj]j\jj[dqiw]j" -Type Directory`. Problem solved. – Ansgar Wiechers Jan 15 '19 at 11:36
-
1@Ansgar Wiechers Thank you. I should have thought of that! It's so easy. – preachers Jan 15 '19 at 11:51
2 Answers
So, 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"

- 19,134
- 9
- 53
- 73
-
It's not a problem if it's a single instance. In my case, however, the `New-Item` cmdlet is in a script with random paths feeding to it. So the escape way won't work. – preachers Jan 15 '19 at 11:55
-
@preachers then you should be able to do `>cd [test]` or `>cd '[test]'` ? – Bakudan Jan 15 '19 at 16:19
-
this helped me deal with an asterisk in a similar use case, thanks! – Edoardo Facchinelli Dec 09 '19 at 05:47
-
That is an effective workaround, but note that (a) it is only needed if `-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](https://github.com/PowerShell/PowerShell/issues/17106) – mklement0 Apr 04 '22 at 21:14
Yes, it is surprising that New-Item
has no -LiteralPath
parameter, especially given that:
its
-Path
parameter behaves like a-LiteralPath
parameter, 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
-LiteralPath
could 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
-Name
argument is also supplied, in which case the-Path
argument 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
-Name
and instead join the name component to the-Path
argument, 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')

- 382,024
- 64
- 607
- 775