New-item with special charachters and long path

Simon C 1 Reputation point

Hi, I recently discovered that with Azure FIle Shares PaaS, if you perform a restore from a backup, and the folder is empty, it does not callback the permissions on the empty folder. I raised this with MS Support and they acknowledge this is an issue they need to resolve but not give an eta. As such, I am wanting to write a script that will look for folders that have no other subfolders or files in it and create an empty file it.

This is what I am using so far:

net use x: '\\\share\folder'
$a = Get-ChildItem -LiteralPath '\\?\x:\' -recurse | Where-Object {$_.PSIsContainer -eq $True -and ($_.GetDirectories().Count -eq 0)}
$path = $a | Where-Object {$_.GetFiles().Count -eq 0} | foreach {$_.FullName}
New-Item -Path $path -force -Name "Touch.txt" -ItemType File

Form the most part this is working fine, however I have come across a couple of issues, 2 of which I have resolves.

  1. Paths to long - I was seeing lots of errors saying that part of the path could not be found. This was due to the path being to long. I have worked around this by mapping a network path using net share to shorten it, and then using -LiteralPath '\?\x:\' which appears to have helped.
  2. Paths with special characters like []. These initially were silently erroring out, so no errors in the terminal, and no new file being created. All other folders were working fine.
    So I pulled one of the paths and performed the New-Item cmd only against that to see what would happen. This is where I started seeing errors saying there were invalid characters.
    Here is an example path -
    '\?\x:\Team Soft\FROM NAME- Name Shared Server and Pooled Server\FROM Name- Name Shared Server and Pooled Server[R] 2013 [F] [ALL R TAGGED][NR] Folder level added by NR as all freestanding documents at this level were NR' (I didn't create this file structure, so don't go there :))
    I was able to stop it erroring by using "" infront of the special characters. So the path now looks like this : '\\?\x:\Team Soft\FROM NAME- Name Shared Server and Pooled Server\FROM Name- Name Shared Server and Pooled Server\[R] 2013[F][ALL R TAGGED]\[NR`] Folder level added by NR as all freestanding documents at this level were NR'
    So now there are no errors, but still the file is not being created.

Further testing and I created some test folders. x:\Team Soft[test]
so with the "" ---- 'x:\Team Soft\[test`]'
It lets me create the file in here fine.

But, this does not work '\?|x:\Team Premier`[test`]'

So it seems that \?\ with the ` causes it to silently error out without creating the file. This is a problem as the long paths I have have special characters, so I kind of need to have both of these things in there to get it to work.

Anyone have any idea how to get around this?... and sorry for the long post, just wanted to explain how I got to where I am.


Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,630 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 35,266 Reputation points

    The file path can have "[" and "]" characters, but they have a special significance. You can use the to denote a range of characters like this: file[123].txt. If you used that in a Get-ChildItem cmdlet you'd get File1.txt, File2.txt, and File3.txt.

    To use the "[" and "]" in the New-Item cmdlet you'll have to separate the file name from the rest of the path literal and use the path part in the -Path parameter and the file name in the -Name parameter.

    When you use the -Path parameter the cmdlet treats the string as if it were the -LiteralPath parameter (which doesn't exist in the New-Item cmdlet) and ignores any "special" characters.

    No comments