Robert
Robert

Reputation: 11

Unable to move forward using cd

I'm having a problem moving forward through a path with PowerShell. I am able to move up the directory but not down. Here's the situation:

I open PowerShell and type in the "pwd" command and it shows that I am currently in PS C:\Users\Robert Inspiron14>

I type the command "cd.." and now I move to PS C:\Users>

I then attempt to change directories by typing: "cd C:\Users\Robert Inspiron14" and I am unable to. Unfortunately, I can't post a picture yet due to lack of reputation.

I'm able to perform the change in CMD but not PowerShell. Also, I don't know how to change the User from "Robert Inspiron14" to just "Robert". Any help is appreciated!

Upvotes: 1

Views: 380

Answers (2)

mklement0
mklement0

Reputation: 437197

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.

  • If the path of the executable (also) requires quoting or is expressed via a a variable, it must be invoked via &, the call operator, for syntactic reasons - see this answer.

  • 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.

Upvotes: 2

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174465

Before PowerShell can execute your cd command, it needs to parse it, and PowerShell's parser interprets your command like this:

cd  C:\Users\Robert Inspiron14
\/  \_____________/ \________/
Command Name  |            |
             argument 1    |
                         argument 2

In other words, C:\Users\Robert and Inspiron14 are interpreted as separate arguments.

Neither argument is a path to a valid directory, so cd (or rather Set-Location for which cd is an alias) throws an error.

You can force PowerShell to recognize C:\Users\Robert Inspiron14 as a single string argument by qualifying its boundaries using quotation marks (both " and ' will work):

cd 'C:\Users\Robert Inspiron14'

You can read more about how PowerShell parses command expressions in the about_Parsing help topic

Upvotes: 2

Related Questions