1

EDIT: I'm on Windows 10 and I think the which command is available because of an installation of Git for Windows.

If I type which nvm in to PowerShell, I get /c/Users/HP/AppData/Roaming/nvm/nvm. If I then want to navigate to that location, /cd c/Users/HP/AppData/Roaming/nvm/nvm fails as path not found.

How can I get a path that I can use with cd please?

  • the command which nvm does not work on my end. Anything I need to install? The path you get is a linux path. Are you on windows? does which offer the ability to return a windows path instead? – LPChip Jul 7 '20 at 10:23
  • Is that a typo or are you missing the leading / in cd c/users...? – DavidPostill Jul 7 '20 at 11:18
  • 1
    [1] which is not a standard windows command. [2] which is not a powershell command. [3] c/users is NOT valid on windows. you need at least the : that goes after the drive letter. something like c:/users. – Lee_Dailey Jul 7 '20 at 13:03
  • 1
    the equivalent command for windows is where.exe ... but you MUST use the full command with the .exe when in powershell since it will otherwise call Where-Object. – Lee_Dailey Jul 7 '20 at 13:07
2

As you are using PowerShell anyway, why not using PowerShell?

The equivalent of which or where.exe in PowerShell is Get-Command. This will return an object. Example:

Get-Command ping

Output:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     PING.EXE                                           10.0.18... C:\Windows\system32\PING.EXE

As you are looking for the path, you need the Path property:

(Get-Command ping).Path

Output:

C:\Windows\system32\PING.EXE

This is at least a path, Windows will understand. But you cannot change your working directory to it as it is not a directory, but an executable. You can again use some PowerShell magic and extract the parent path of the executable:

Split-Path (Get-Command ping).Path

Output:

C:\Windows\system32

You can then use this path to change your working directory. Btw, cd in PowerShell is an alias for Set-Location which you can verify with Get-Command cd. In conclusion you can pipe your result to cd or to Set-Location and get your desired solution in one line (prompt not stripped to see the result):

PS C:\> Split-Path (Get-Command ping).Path | cd
PS C:\Windows\system32>
  • +1 But Split-Path (Get-Command ping).Source didn't work for me. However, looking at the official documentation for Get-Command, using Definition in place of Source did (so Split-Path (Get-Command ping).Definition). – Anaksunaman Jul 8 '20 at 0:20
  • @Anaksunaman Thanks for the hint. I checked the docs: I think it would even better to use the Path property. Does that work on your machine? Which OS and PS version are you using btw.? – Thomas Jul 8 '20 at 5:39
  • "Does that work on your machine? " - Yes. Split-Path (Get-Command ping).Path does the job. =) For reference, test machine is running Windows 7 with PowerShell 3.0. – Anaksunaman Jul 8 '20 at 19:56
0

I think the which command is available because of an installation of Git for Windows.

As alluded to in the comments, Git for Windows does not use the same path format as PowerShell (or Windows broadly). The which command you are using is apparently returning a path meant to be used in ex. Git Bash.

Of particular note:

  • PowerShell and Windows do not typically use forward slashes (/) as path separators. Instead, backwards slashes \ are used natively.

  • Drive letter are not specified as /drive/. They are specified as drive:\.

Bearing this in mind, the equivalent path to:

/c/Users/HP/AppData/Roaming/nvm/nvm

in PowerShell is:

c:\Users\HP\AppData\Roaming\nvm\nvm.ext

Note that ext. in this case is just meant at a generic extension (likely .exe). Also, /cd is likely not valid and the last nvm is likely the executable itself (minus an extension i.e. whatever ext might be). So you can just use i.e.:

cd c:\Users\HP\AppData\Roaming\nvm

How can I get a path that I can use with cd please?

As noted several times elsewhere, where in cmd (a normal command window) and where.exe (in PowerShell) will return a correctly formatted path for Windows such as e.g. c:\Users\HP\AppData\Roaming\nvm\nvm.ext above (so either where nvm in cmd or where.exe nvm in PowerShell). Just drop the final executable name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.