Issue with path containing [ and ( when using -Path

Viewed 63

I try to write a script with PowerShell to move 3 folders. I've got some issue due to the different [] and () in my path. I can't change the path and want to find a solution.

The goal is to check where the 3 folders are and to change their position.

Here's my code:

    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

if(((Test-Path "D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\[TMX] IF2 Red Shed") -eq $True ) -and ((Test-Path "D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\[CP] Immersive Farm 2") -eq $True ) -and ((Test-Path "D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\[FTM] Immersive Farm 2 Forage+Ore") -eq $True ))
    {
    Move-Item -Path D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\[TMX] IF2 Red Shed -Destination D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement
    Move-Item -Path D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\[CP] Immersive Farm 2 -Destination D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement
    Move-Item -Path D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\[FTM] Immersive Farm 2 Forage+Ore -Destination D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement
    }
Elseif(((Test-Path "D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement\[TMX] IF2 Red Shed") -eq $True ) -and ((Test-Path "D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement\[CP] Immersive Farm 2") -eq $True ) -and ((Test-Path "D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement\[FTM] Immersive Farm 2 Forage+Ore") -eq $True ))
    {
    Move-Item -Path D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement\[TMX] IF2 Red Shed -Destination D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods
    Move-Item -Path D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement\[CP] Immersive Farm 2 -Destination D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods
    Move-Item -Path D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Map immersive\dossier de changement\[FTM] Immersive Farm 2 Forage+Ore -Destination D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods
    }
Else
    {
    $oReturn=[System.Windows.Forms.Messagebox]::Show("No folders")
    }
2 Answers

Use -LiteralPath instead

When you use -Path with the Item/ChildItem provider cmdlets, PowerShell treats the argument as a potential wildcard pattern!

Since [A-Z] is a wildcard pattern construct, the file system globber won't actually resolve file(s) with a literal [ or ] in the name.

Using -LiteralPath will supress any attempt to expand wildcards:

Test-Path -LiteralPath "D:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\Mods\[TMX] IF2 Red Shed"

Remember to quote paths with spaces as well!

Move-Item -LiteralPath 'C:\path with [wildcards]\in\the\name.ext' -Destination 'C:\Destination\path\'

I found through tab completion that you can escape the square brackets with backquotes:

test-path '.\a`[hi`]\'
move-item '.\a`[hi`]\' foo
set-location '.\a`[hi`]\'
Related