-1

How does powershell find variable paths? For example like this:

cmd: where node
C:\Windows\System32\where.exe
weiya ou
  • 2,730
  • 1
  • 16
  • 24
  • 1
    Your question is a bit unclear - do you mean, when you type "where.exe" and hit enter, how does PowerShell know where to _find_ that executable on the file system? – Mathias R. Jessen Jul 17 '20 at 16:31
  • 1
    I'm not sure what you're trying to ask. **The `where` command, which should work exactly the same in powershell console as in cmd**, used like you have, will retrieve files, in the current directory, and the locations defined in the user, and system `%PATH%` variables, which are named `node`, or named `node` and have an extension which is listed under the variable `%PATHEXT%`. – Compo Jul 17 '20 at 16:33
  • 3
    @Compo Windows PowerShell includes `where -> Where-Object` alias, so you'll have to issue `where.exe` in powershell to make it work – Mathias R. Jessen Jul 17 '20 at 16:36
  • 1
    Of course @MathiasR.Jessen, perhaps my comment was confusing, I wasn't advocating the use of `where node` when I said 'like you have'. I would never invoke an executable file without using its extension, regardless of the language I am coding with. In cmd, I would have used `%__AppDir__%where.exe node.exe` or for a full drive search `%__AppDir__%where.exe /R C:\ node.exe` – Compo Jul 17 '20 at 16:40

2 Answers2

2

Aside from running where.exe, I would use get-command instead of where.exe:

get-command notepad
gcm notepad # | ft -a

CommandType Name        Version        Source
----------- ----        -------        ------
Application notepad.exe 10.0.18362.693 C:\Windows\system32\notepad.exe

If you really don't like the where alias:

remove-item alias:where -force
js2010
  • 23,033
  • 6
  • 64
  • 66
1

Usually, when you invoke where.exe, it will first look in the current directory for the executable. Then it will search in the directories located in PATH Environment variable. And if any extension is not specified, it will search in the extensions for PATHEXT environment variable. So when you use where node it will first search in current directory and PATH directories with enumerating all extension in PATHEXT like node.exe, node.bat, node.vbs etc. You can assign more directories to PATH Using setx PATH "%path%;Directory" and to PATHEXT like setx PATHEXT "%pathext%;Extension from command prompt. Also powershell has a where.exe native alternative:

(Get-Command node).Definition

Will expand to the full path of found "node" in known directories. This method also works in the same way as where.exe.

Also one thing I cannot understand how simply typing where in powershell worked. Where and ? Are aliases for where-object. So to execute where.exe you need to run & where.exe param_list

(Note: as @Compo said in the comments, if you search for a file named "node.txt", and if anything named "node.txt.exe" exists, then it will match "node.txt.exe" so beware of double-extensions.)

Wasif
  • 14,755
  • 3
  • 14
  • 34
  • Just to correct you Wasif_Hasan. This is incorrect, `And if any extension is not specified, it will search in the extensions for PATHEXT environment variable.`. If you search for `node.txt`, a file named `node.txt.exe` will match because the `.EXE` extension listed under `%PATHEXT%` will be appended, regardless of the fact you included an extension yourself. – Compo Jul 17 '20 at 16:54
  • Thanks @Compo for the excellent note, incorporating it in my answer. – Wasif Jul 17 '20 at 17:08
  • I would also advise not to use `setx` like that for modifying the `%PATH%` content. The content of `%PATH%` is special in that it is an amalgamation of the content of both the user `%PATH%`, variable and the system `%PATH%` variable. When you use `SetX PATH "%path%;Directory"`, as you're not using the `/M` option for the `system` environment, you are adding the content of both the system and user environments, `%PATH%` variables plus `Directory` to the user `%PATH%` environment variable, which is not a desired result, as you'll now have multiple duplicates. – Compo Jul 17 '20 at 17:36
  • 1
    Please also note that `Get-Command` is a little bit different in that is finds much more than applications, so in order to retrieve `node.exe`, you should provide the type of command you're looking for, i.e. `(Get-Command node -CommandType Application).Source`, or using aliases, `(gcm node -type application).source`, or even lazier `(gcm node -c ap).source`. – Compo Jul 17 '20 at 18:01