Search code examples
powershellspecial-characterscd

powershell cannot change directory if path contains [ whereas dos command can


I don't understand why I cannot in PowerShell go into a folder with [], for example

cd c:\test\[demo] 

whereas I have created in PowerShell with

md [demo]

and I can actually cd with DOS command.

So what can I do if I want to navigate in this folder from PowerShell ?


Solution

  • cd in PowerShell is an alias to Set-Location which receives a -Path by default if no option is specified. Almost all PowerShell commands that deal with files like Get-ChildItem, Get-Acl... use the FileSystem Provider and has -Path as a wildcard pattern where [] has special meaning. You'll need to escape those special characters like this

     cd '`[demo`]'
    

    But cmdlets that receives wildcards also have -LiteralPath and that's the correct way to do

    cd -LiteralPath [demo] # Or
    cd -L [demo]