What should I do if the -Path parameter in New-Item can't handle "[" or "]" characters?

To handle cases where the path contains the characters “[” or “]” without the -LiteralPath parameter in New-Item, how should I proceed?

$m = 'd:\temp\新建文件夹\[A]\001.jpg'
$n = 'd:\temp\新建文件夹\[B]\001.jpg'

New-Item -ItemType HardLink -Path $n -Target $m

------------------------------------------------------------

Line |
   4 |  New-Item -ItemType HardLink -Path $n -Target $m
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot find path 'd:\temp\新建文件夹\[A]\001.jpg' because it does not exist.

I tried adding the escape character \ in the path, but it didn’t work.

$m = 'd:\temp\新建文件夹\\[A\]\001.jpg'

Does that file exist for $m?
-Target is what link is pointing to so target should already exist and $n (-Path) is the new hardlink

The escape character in powershell is the ` character.

$m = 'd:\temp\新建文件夹\`[A`]\001.jpg'
$n = 'd:\temp\新建文件夹\`[B`]\001.jpg'

New-Item -ItemType HardLink -Path $n -Target $m

[ and ] are accepted chars in file paths

yes but the *-item cmdlets don’t work with them outside of -LiteralPath. Try it yourself.

Hmmm worked when I tested

“I added the backtick () character, and oddly, it works correctly with Test-Pathto find the file, but not withNew-Item`.”

# -Target
$m = 'd:\temp\新建文件夹\`[A`]\001.jpg'

# -path
$n = 'd:\temp\新建文件夹\`[B`]\001.jpg'

if(Test-Path -Path $m){
    Write-Host "Yes"
}else{
    Write-Host "No"
}

New-Item -ItemType HardLink -Path $n -Target $m
PS C:\Users\12940\Desktop\windows_script> . 'C:\Users\12940\Desktop\windows_script\powershell\dome1.ps1'

--------------------------------------------------

-----Yes------
New-Item: C:\Users\12940\Desktop\windows_script\powershell\dome1.ps1:13:1
Line |
  13 |  New-Item -ItemType HardLink -Path $n -Target $m
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Could not find item d:\temp\新建文件夹\`[A`]\001.jpg.

$m exists. I tried adding the backtick (), and while Test-Pathcan find the file,New-Item` cannot.

# -Target
$m = 'd:\temp\新建文件夹\`[A`]\001.jpg'

# -path
$n = 'd:\temp\新建文件夹\`[B`]\001.jpg'

if(Test-Path -Path $m){
    Write-Host "-----Yes------"
}else{
    Write-Host "-----No-------"
}

New-Item -ItemType HardLink -Path $n -Target $m

------------------------------
PS C:\Users\12940\Desktop\windows_script> . 'C:\Users\12940\Desktop\windows_script\powershshell\dome1.ps1'
-----Yes------
New-Item: C:\Users\12940\Desktop\windows_script\powershell\dome1.ps1:13:1
Line |
  13 |  New-Item -ItemType HardLink -Path $n -Target $m
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Could not find item d:\temp\新建文件夹\`[A`]\001.jpg.

It worked in my test. The only difference is the asian characters. Maybe those are causing similar issue?

I have replaced ‘新建文件夹’ with ‘folder,’ but it still doesn’t work.

PS C:\Users\12940\Desktop\windows_script> . 'C:\Users\12940\Desktop\windows_script\powershell\dome1.ps1'
-----Yes------
New-Item: C:\Users\12940\Desktop\windows_script\powershell\dome1.ps1:13:1
Line |
  13 |  New-Item -ItemType HardLink -Path $n -Target $m
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Could not find item D:\temp\folder\`[A`]\001.jpg.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.