Search code examples
powershellpathcdtrailing-whitespace

How to use Set-Location on a folder with trailing spaces in PowerShell?


I have mounted a linux shared folder. In the folder, there is a sub-folder . \ (dot space) which I need to access. With the command prompt I can access it using the 8dot3 notation of short names, but I need to access it with PowerShell.

Set-Location and cd will throw an error on paths with folder names with trailing spaces (path does not exist).


Solution

  • Windows still does not have the best support for folder names with trailing spaces. You can use a workaround with symbolic links. Create a symbolic link to the folder that contains a trailing space in its name using the mklink command of an elevated Windows command prompt (not available in PS as it is a command and not a tool) and define your path as a UNC path:

    mklink /D C:\MyLink "\\?\C:\path\to\folder\. "
    

    After that you can do:

    cd C:\MyLink
    

    or:

    Set-Location -LiteralPath C:\MyLink
    

    in PowerShell to work from your directory that has a trailing space in its name.

    You can read more on operations with folder names containing trailing spaces in my answer here.