tl;dr
Since your directory name contains spaces, you must quote it, e.g.:
# Note: In PowerShell, 'cd' is an alias of 'Set-Location'
cd 'Program Files'
Alternatively, if the intent is to change to the well-known programs folder (typically,
C:\Program Files
), you may reference the relevant environment variable, in which case quoting is not required:[1]
cd $env:ProgramFiles
instead of cd 'C:\Program Files'
The following discusses how to pass string literals with spaces as single arguments.
As stated in Maximilian Burszley's helpful answer, Program Files
is parsed as two arguments, because spaces are used to separate command-line arguments.
Your attempt to use cd Program Files
may be inspired by cmd.exe
(the legacy Command Prompt), where this syntax indeed works; however, even there it conceptually violates the usual rules of argument parsing.
Therefore, you need to use a form of quoting in order to pass a value that contains spaces as a single argument.
You have several options to implement this quoting:
Use '...'
around values that are literals, i.e., that should be used verbatim; a '...'
string is called a verbatim (single-quoted) string.
Use "..."
around values in which you want to embed variable references (e.g., $HOME
) or subexpressions (e.g., $(Get-Date)
; that is, "..."
strings perform string interpolation, and they're called expandable (double-quoted) strings.
Use `
to quote (escape) a single character; `
, the so-called backtick, is PowerShell's general escape character.
Therefore, you could use any of the following:
cd 'Program Files'
cd "Program Files" # as there are no $-prefixed tokens, same as 'Program Files'
cd Program` Files # `-escape just the space char.
Also, you can use tab-completion to expand the (space-less) prefix of a space-containing path to its full form with quoting applied implicitly.
E.g., if you're in C:\
and you type:
cd Program<tab>
PowerShell automatically completes the command to:
cd '.\Program Files\'
Note how the Program Files
(along with .\
to refer to the current dir. and a trailing \
to indicate a dir.) was automatically single-quoted.
Using wildcard expressions as arguments:
As noted, in PowerShell cd
is a built-in alias of the Set-Location
cmdlet.
Passing a path positionally - e.g. Set-Location 'C:\Program Files'
- implicitly binds it to the -Path
parameter; that is it, is equivalent to Set-Location -Path 'C:\Program Files'
-Path
interprets its argument as a wildcard expression, so that you can do something like Set-Location C:\Win*
in order to change to C:\Windows
(assuming that the wildcard expression matches only one directory).
The tricky thing is that - unlike in cmd.exe
- it isn't just *
and ?
that have special meaning in PowerShell wildcard expressions, but [
and ]
as well (for character-set and character-range expressions such as [abc]
and [a-c]
), so that Set-Location Foo[1]
will not work for changing to a directory literally named Foo[1]
.
In that case, you must use the -LiteralPath
parameter -
Set-Location -LiteralPath Foo[1]
- to ensure that the path is interpreted literally (verbatim).
[1] This even applies if you append a space-less literal, e.g. cd $env:ProgramFiles\Git
; however, if the literal part contains spaces, quoting is again needed; while, e.g., cd $env:ProgramFiles\'Common Files'
works, the preferable approach is to enclose the entire argument in "..."
: cd "$env:ProgramFiles\Common Files"
cd 'Program Files'
– Mcardle[tab]
(repeatedly) to cycle through names starting with those chars. Quotes and `.` prefix are added automatically as required. – Recollected