3

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?

5
  • > 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 Commented Jan 15, 2019 at 11:20
  • You can have square brackets in the item name....: New-Item -Path "/the/path/to/your[file]here.txt" -ItemType Directory Commented Jan 15, 2019 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 Commented Jan 15, 2019 at 11:32
  • 3
    @preachers New-Item -Path "G:\1\jj[jj]j\jj[dqiw]j" -Type Directory. Problem solved. Commented Jan 15, 2019 at 11:36
  • 1
    @Ansgar Wiechers Thank you. I should have thought of that! It's so easy. Commented Jan 15, 2019 at 11:51

2 Answers 2

2

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.
  • 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.
    • The workaround is to not use -Name and instead join the name component to the -Path argument, such as with Join-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')
Sign up to request clarification or add additional context in comments.

3 Comments

The workaround doesn't work if the name is unrelated to the value eg. New-Item -Type SymbolicLink -Name 'myShortcut' -Value '../to[another].file' (as mentioned in your #17106)
@Hashbrown, in case you're referring to the conceptually related, but separate issue of the -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.
To add insult to injury, the current workaround for treating -Value literally is to escape the wildcard metacharacters twice(!): New-Item -Type SymbolicLink -Name 'myShortcut' -Value '../to``[another``].file' - see GitHub issue #14534
1

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"

4 Comments

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 then you should be able to do >cd [test] or >cd '[test]' ?
this helped me deal with an asterisk in a similar use case, thanks!
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.