To complement Mathias R. Jessen's helpful answer with more background information:
Quoting an argument that contains spaces is a general syntactic necessity, in all shells, because unquoted spaces are used to separate multiple arguments.
It isn't only spaces that require quoting, but any of PowerShell's so-called metacharacters (characters that, when used unquoted, have syntactic function); for instance, passing the path to a directory literally named a;b
requires quoting as well, given that ;
would otherwise be interpreted as a statement separator.
- Note that PowerShell has more metacharacters than both
cmd.exe
; for the complete list, see this answer.
There are multiple quoting styles:
Since your path value is a literal - it contains no variable references or expressions - a verbatim (single-quoted) string ('...'
) is the best choice.
cd 'C:\Users\Robert Inspiron14'
If your path contains variables or subexpressions, you must use an expandable (double-quoted) string ("..."
)[1]
cd "$HOME\Documents"
Another, less common solution is to individually escape the space characters with `
, the so-called backtick, PowerShell's escape character:
cd C:\Users\Robert` Inspiron14
Also note:
PowerShell's tab-completion automatically applies quoting as necessary.
cd..
is the name of a built-in function in PowerShell, whose sole purpose is to emulate cmd.exe
's (questionably permissive) behavior (see below); the function performs a syntactically correct Set-Location ..
call (verify by executing ${function:cd..}
), with a space separating the command name from its argument.
Contrast with cmd.exe
:
Unfortunately, cmd.exe
's built-in cd
command decided not to enforce its usual syntax rules, and enabled calls such as cd C:\Program Files
.
It should never have done that: While convenient at first glance, it constitutes a problematic exception from the usual rules that invites confusion.
Note that cmd.exe
's tab completion properly quotes arguments that contain spaces.
Similarly, cd..
was unfortunately allowed as as syntactically exceptional alternative to the correct cd ..
- see the comments on this answer for details.
[1] Note "..."
-quoting isn't strictly necessary if you use variable references in a path, as long as any literal components do not require quoting; e.g., $HOME\foo
is fine without quoting, whereas the "
around "$HOME\foo bar"
are required. With subexpressions ($(...)
), the rules get more complicated, so the simplest approach is to always use "..."
-quoting with them.