Home > Enterprise >  PowerShell fails to interpret commands such as "which"
PowerShell fails to interpret commands such as "which"

Time:02-16

My windows Powershell gives the expected results when interpreting commands such as "date" and "echo", while problems occur when it interprets such commands as "which" and "tail". I thought it might be that I haven't add the address of these commands to the target directories, but where can I probably find these commands.

What happens when I apply "which", with the same thing happening when applying "tail"

CodePudding user response:

which and tail are the names of external programs, which are unrelated to PowerShell. These programs can only be expected to be present on Unix-like platforms, not (natively) on Windows. (However, they would be present in Unix-like subsystems on Windows, such as WSL.)

By contrast, date and echo are (potentially) commands that are built into PowerShell, irrespective of what platforms it runs on:

  • echo is an alias of PowerShell's Write-Output cmdlet on all platforms.

  • date - unless an external program by that name is present in a directory listed in $env:PATH on a given platform - refers to the built in Get-Date cmdlet.

    • Note: This relies on PowerShell's ill-conceived default verb feature, which falls back on prefixing a command name with Get- if no command form is found by the given name. This shouldn't be relied upon, both in the interest of conceptual clarity and to avoid unnecessary overhead. Also, this fallback is (unexpectedly) not reported by the Get-Command discussed below, and also not by Get-Help - see GitHub issue #3987.

Use Get-Command to determine what command form, if any, a given name refers to (add -All to see if potentially multiple forms exist, with the effective one being listed first).

  • Related