Issue with path containing [ and ( when using -Path

前端 未结 2 1542

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

相关标签:
2条回答
  • 2020-12-12 02:23

    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`]\'
    
    0 讨论(0)
  • 2020-12-12 02:36

    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\'
    
    0 讨论(0)
提交回复
热议问题