
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, you must “escape”.
Command Line 101: Why You Must Escape Spaces
A character’s “escape” changes its meaning. For example, escaping a space will cause the shell to treat it as a standard space character rather than a special character that separates command line arguments.
For example, suppose you have a text file whose contents you want to see. You can do this with the type command. Assuming the text file is in C: Test File.txt, the following command in the command prompt will display its contents:
type C: Test File.txt
Awesome. Now what if you have the same file in C: Test Folder Test File.txt? If you try to run the command below, it won’t work. These spaces in the file path are annoying.
type C: Test Folder Test File.txt
The command line thinks you are trying to search for a file called C: Test and says it “cannot find the path specified”.
Three ways to escape spaces in Windows
There are three different ways to escape file paths on Windows:
By enclosing the path (or parts of it) in double quotes (”).
By adding a cursor character (^) before each space. (This only works in Command Prompt / CMD, and it doesn’t seem to work with all commands.)
By adding a grave accent character (`) before each space. (This only works in PowerShell, but it still works.)
We will show you how to use each method.
Insert the path in quotes (“)
The standard way to ensure that Windows handles a file path correctly is to enclose it in double quotes (”). For example, with our sample command above, we’ll just run the following command instead:
type “C: Test Folder Test File.txt”
You can actually put parts of the path in quotes if you prefer. For example, suppose you have a file named File.txt in this folder. You can do the following:
type C: “Test folder” File.txt
However, this is not necessary. In most cases, you can just use quotes all around the path.
This solution works in both the traditional Command Prompt Environment (CMD) and in Windows PowerShell.
Sometimes: use the Caret character to escape spaces (^)
In the command prompt, the insertion character (^) will allow you to escape spaces – in theory. Just add it before each space in the filename. (You will find this character in the number line of your keyboard. To enter the insertion character, press Shift + 6.)
Here’s the problem: while it should work, and it works sometimes, it doesn’t work all the time. The handling of this character through the command prompt is strange.
For example, with our sample command, you would run the following command and it wouldn’t work:
type C: Test ^ Folder Test ^ File.txt
On the other hand, if we try to open our file directly by typing its path in the command prompt, we can see that the caret character correctly escapes spaces:
C: Test ^ Folder Test ^ File.txt
So when does it work? Well, based on our research, it seems to work with some apps and not others. Your mileage may vary depending on the control you use. The handling of this character through the command prompt is strange. Try it with whatever command you use, if you’re interested it may or may not work.
For consistency, we recommend that you stick to double quotes in the command prompt or switch to PowerShell and use the grave accent method below.
PowerShell: use the grave accent character (`)
PowerShell uses the grave character (`) as an escape character. Just add it before each space in the filename. (You’ll find this character above the Tab key and under the Esc key on your keyboard.)
type C: Test` Folder Test` File.txt
Each grave accent tells PowerShell to escape the next character.
Note that this only works in the PowerShell environment. You will need to use the caret character in the command prompt.
If you are familiar with UNIX-like operating systems like Linux and macOS, you may be used to using the backslash () before a space to escape it. Windows uses it for normal file paths, so it doesn’t work – the caret (^) and grave accent (`) characters are the Windows version of the backslash, depending on which command-line shell you’re using.