How to escape spaces in file paths on the Windows command line

Contents

Command Prompt Window in Windows 10

Command line environments like Windows command prompt and PowerShell use spaces to separate commands and arguments, but file and folder names can also contain spaces. To specify a file path with a space inside, shall “escape”.

Command line 101: Why must you escape the spaces

“Escape” of a character changes its meaning. As an example, escaping a space will cause the shell to treat it as a standard space character rather than a special character that separates the command line arguments.

As an example, suppose you have a text file whose content you want to see. You can do it with the type command. Assuming the text file is in C:TestFile.txt, the following command at the command prompt will display its content:

type C:TestFile.txt

Excellent. Now, What if you have the same file in C:Test FolderTest File.txt? If you try to run the following command, it will not work: those spaces in the file path get in the way.

type C:Test FolderTest File.txt

The command line thinks you are trying to find a file called C:Test and says that “the specified path cannot be found”.

Command prompt error when spaces are not escaped

Three alternatives to escape spaces in Windows

There are three different ways to escape file paths in Windows:

  • Including the path (or parts of it) in double quotes (”).
  • Adding a caret (^) before each space. (This only works in command prompt / CMD, and it doesn't seem to work with all commands).
  • Adding a Grave Accent Character (`) before each space. (This only works in PowerShell, but it always works).

We will show you how to use each method.

Enclose the path in quotes (“)

The standard way to ensure that Windows treats a file path correctly is to enclose it in double quotes (”). As an example, with our sample command above, we would simply run the following instead:

type "C:Test FolderTest File.txt"

Actually, you can enclose parts of the path in quotes if you prefer. As an example, let's say you have a file called File.txt in that folder. You could run the following:

type C:"Test Folder"File.txt

Despite this, That is not necessary; In most cases, you can use quotes throughout the path.

This solution works both in the traditional command prompt environment (CMD) as in Windows PowerShell.

Include spaces in double quotes at the command prompt

Sometimes: use the Caret character to escape spaces (^)

At the command prompt, the caret (^) will allow you to escape the spaces, in theory. Just add it before each space in the filename. (You will find this character in the number row of your keyboard. To type the caret, press Shift + 6).

Here is the problem: although this should work, and sometimes it does, it doesn't work all the time. The command prompt handling of this character is strange.

As an example, with our sample command, I would run the following and it wouldn't work:

type C:Test^ FolderTest^ File.txt

Caret space escape error at command prompt

On the other hand, if we try to open our file directly by typing its path at the command prompt, we can see that the caret escapes spaces correctly:

C:Test^ FolderTest^ File.txt

Caret space escaping working at command prompt

Then, When does it work? Well, according to our research, seems to work with some apps and not others. Your mileage may vary depending on the command you are using. The command prompt handling of this character is strange. Try it with whatever command you are using, if you are interested, it may or may not work.

To maintain consistency, We suggest you stick to the double quotes at the command prompt, or switch to PowerShell and use the grave accent method below.

PowerShell: use the grave accent character (`)

PowerShell uses the grave accent character (`) as escape character. Just add it before each space in the filename. (You will find this character above the Tab key and below the Esc key on your keyboard).

type C:Test` FolderTest` File.txt

Each grave accent character tells PowerShell to escape the next character.

Please note this only works in PowerShell environment. You will have to use caret in command prompt.

Escape Grave Accented Spaces in PowerShell


If you are familiar with UNIX-like operating systems such as Linux and macOS, you may be used to using the backslash character () before a space to escape. Windows uses this for normal file paths, so it doesn't work; accent characters (^) and grave accent (`) are the Windows version of the backslash, depending on the command line shell you are using.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.