在PowerShell中使用where命令查找文件

CMD中的where命令

where命令显示符合搜索模式的文件位置。在默认情况下,搜索是在当前目录和 PATH 环境变量指定的路径中执行的。
where命令对应文件的路径为C:\Windows\System32\where.exe

where命令案例

C:\Users\Administrator>where python
C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe

where命令帮助

C:\Users\Administrator>where /?

WHERE [/R dir] [/Q] [/F] [/T] pattern...

描述:
    显示符合搜索模式的文件位置。在默认情况下,搜索是在当前目录和 PATH
    环境变量指定的路径中执行的。

参数列表:
    /R       从指定目录开始,递归性搜索并显示符合指定模式的文件。

    /Q       只返回退出代码,不显示匹配文件列表。(安静模式)

             匹配文件。(安静模式)

    /F       显示所有相配文件并用双引号括上。

    /T       显示所有相配文件的文件的文件。

    pattern  指定要匹配的文件的搜索模式。通配符 * 和 ? 可以用在模式中。
             也可以指定 "$env:pattern""path:pattern" 格式; 其中
             "env" 是环境变量,搜索是在 "env" 变量的指定的路径中执行的。
             这些格式不应该跟 /R 一起使用。此搜索也可以用将 PATHEXT 变
             量扩展名附加于此模式的方式完成。

     /?      显示此帮助消息。

  注意: 如果搜索成功,此工具返回错误级别 0; 如果不成功,返回 1; 如果失
        败或发生错误,返回 2。

示例:
    WHERE /?
    WHERE myfilename1 myfile????.*
    WHERE $windir:*.*
    WHERE /R c:\windows *.exe *.dll *.bat
    WHERE /Q ??.???
    WHERE "c:\windows;c:\windows\system32:*.dll"
    WHERE /F /T *.dll

PowerShell中的where命令

PowerShell中执行where命令无效。

PS C:\Users\Administrator> where python.exe

根据如下命令可知,在PowerShellwhere命令对应的是Where-Object命令,优先级比where.exe高。CMD里的where命令本来全名就是where.exe,只不过可以省略.exe而已。所以实现需要CMD里的where命令功能显式地使用where.exe

PS C:\Users\Administrator> gcm where -All

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           where -> Where-Object
Application     where.exe                                          10.0.17... C:\Windows\system32\where.exe

PowerShell中执行where.exe命令有效。

PS C:\Users\Administrator> where.exe python.exe
C:\ProgramData\Anaconda3\python.exe
C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe

PowerShell中类似where的命令

PowerShell中的gcmget-command命令与CMD中的where命令相似,注意加上-All才会显示所有满足要求的文件。

PS C:\Users\Administrator> gcm python.exe -All

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     python.exe                                         3.8.815... C:\ProgramData\Anaconda3\python.exe
Application     python.exe                                         3.7.215... C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe

gcm命令是get-command命令的别名。

PS C:\Users\Administrator> get-command python.exe -All

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     python.exe                                         3.8.815... C:\ProgramData\Anaconda3\python.exe
Application     python.exe                                         3.7.215... C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe

总结

PowerShell中使用类似where命令的查找文件功能的方法有三种:

  • where.exe 文件搜索字符串
  • gcm 文件搜索字符串 -All
  • get-command 文件搜索字符串 -All

猜你喜欢

转载自blog.csdn.net/mighty13/article/details/119880762