You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell执行which命令报错:‘which’未被识别为有效命令,如何解决?

Fixing "which : The term 'which' is not recognized" in PowerShell

Hey there! I’ve dealt with this exact frustration before—PowerShell doesn’t come with the which command built-in like Unix-based shells (Bash, Zsh, etc.) do. No worries though, there are a few easy ways to get the same functionality:

  • Use PowerShell’s native Get-Command (or its alias gcm)
    This is the official PowerShell replacement for which. To find the path of a command, run:

    Get-Command xxxxxx
    

    Or use the shorter alias for quicker typing:

    gcm xxxxxx
    

    If you only want to see the raw executable file path (and skip extra metadata), pipe the output to narrow it down:

    gcm xxxxxx | Select-Object -ExpandProperty Source
    

    You can also filter results to only show applications (ignoring scripts or aliases) with:

    gcm xxxxxx -CommandType Application
    
  • Create a custom which function (to keep using the familiar syntax)
    If you’d rather stick with typing which like you do in Unix shells, you can add a simple function to your PowerShell profile. Here’s how:

    1. Open your PowerShell profile file with:
      notepad $PROFILE
      
      (If the file doesn’t exist, run New-Item -Path $PROFILE -ItemType File first to create it.)
    2. Paste this function into the file:
      function which {
          Get-Command -Name $args[0] -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source
      }
      
    3. Save the file, then restart PowerShell or run . $PROFILE to load the changes right away. Now you can run which xxxxxx just like you’re used to!
  • Use Windows’ built-in where.exe
    Windows has its own native tool for locating executable files, which works almost exactly like which. Run:

    where.exe xxxxxx
    

    This will return the full path of any matching executable in your system’s PATH variable.

The root cause of that error is straightforward: which isn’t a native PowerShell cmdlet—it’s exclusive to Unix-like environments. All the options above give you the same (or better) functionality directly in PowerShell.

内容的提问来源于stack exchange,提问作者wtian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:57:45