当我开始在PowerShell中自定义配置文件时,第一个别"/>

关于unix:相当于* Nix’在PowerShell中的命令?

关于unix:相当于* Nix’在PowerShell中的命令?

Equivalent of *Nix 'which' command in PowerShell?

如何询问PowerShell中的内容?

例如,"which notepad",它返回根据当前路径运行notepad.exe的目录。


当我开始在PowerShell中自定义配置文件时,第一个别名是"which"。

1
New-Alias which get-command

要将此添加到您的配置文件,请键入:

1
"`nNew-Alias which get-command" | add-content $profile

最后一行开头的"n"用于确保它将以新行开头。


这里是一个实际的*nix等价物,即它提供*nix样式的输出。

1
Get-Command <your command> | Select-Object -ExpandProperty Definition

换上你要找的东西就行了。

1
2
3
PS C:\> Get-Command notepad.exe | Select-Object -ExpandProperty Definition
C:\Windows\system32
otepad.exe

将其添加到配置文件时,您将希望使用函数而不是别名,因为不能对管道使用别名:

1
2
3
4
function which($name)
{
    Get-Command $name | Select-Object -ExpandProperty Definition
}

现在,重新加载配置文件时,可以执行以下操作:

1
2
3
PS C:\> which notepad
C:\Windows\system32
otepad.exe


我通常只打:

1
gcm notepad

1
gcm note*

gcm是get命令的默认别名。

在我的系统上,GCM注*输出:

1
2
3
4
5
6
7
8
9
10
11
12
[27] ? gcm note*

CommandType     Name                                                     Definition
-----------     ----                                                     ----------
Application     notepad.exe                                              C:\WINDOWS
otepad.exe
Application     notepad.exe                                              C:\WINDOWS\system32
otepad.exe
Application     Notepad2.exe                                             C:\Utils
otepad2.exe
Application     Notepad2.ini                                             C:\Utils
otepad2.ini

您将得到与您要查找的内容相匹配的目录和命令。


试试这个例子:

1
(Get-Command notepad.exe).Path

我的建议是:

1
2
3
4
5
function which($cmd) { get-command $cmd | % { $_.Path } }

PS C:\> which devcon

C:\local\code\bin\devcon.exe

检查此PowerShell。

这里提供的代码表明:

1
($Env:Path).Split(";") | Get-ChildItem -filter notepad.exe


这似乎是您想要的(我在http://huddledmass.org/powershell find path/上找到的):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Function Find-Path($Path, [switch]$All = $false, [Microsoft.PowerShell.Commands.TestPathType]$type ="Any")
## You could comment out the function stuff and use it as a script instead, with this line:
#param($Path, [switch]$All = $false, [Microsoft.PowerShell.Commands.TestPathType]$type ="Any")
   if($(Test-Path $Path -Type $type)) {
      return $path
   } else {
      [string[]]$paths = @($pwd);
      $paths +="$pwd;$env:path".split(";")

      $paths = Join-Path $paths $(Split-Path $Path -leaf) | ? { Test-Path $_ -Type $type }
      if($paths.Length -gt 0) {
         if($All) {
            return $paths;
         } else {
            return $paths[0]
         }
      }
   }
   throw"Couldn't find a matching path of type $type"
}
Set-Alias find Find-Path

与unix which的快速而肮脏的匹配是

1
New-Alias which where.exe

但如果存在多行,则返回多行,因此

1
$(where.exe command | select -first 1)

在Windows 2003或更高版本上尝试使用where命令(如果安装了资源工具包,请使用Windows 2000/XP)。

顺便说一句,这在其他问题上得到了更多的答案:

Windows上是否有"which"的等效项?

PowerShell相当于unix which命令?


我喜欢Get-Command | Format-List,或更短,使用两个别名,仅用于powershell.exe

1
gcm powershell | fl

您可以找到这样的别名:

1
alias -definition Format-List

tab完成与gcm的工作。


我在PowerShell配置文件中具有此which高级功能:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function which {
<#
.SYNOPSIS
Identifies the source of a PowerShell command.
.DESCRIPTION
Identifies the source of a PowerShell command. External commands (Applications) are identified by the path to the executable
(which must be in the system PATH); cmdlets and functions are identified as such and the name of the module they are defined in
provided; aliases are expanded and the source of the alias definition is returned.
.INPUTS
No inputs; you cannot pipe data to this function.
.OUTPUTS
.PARAMETER Name
The name of the command to be identified.
.EXAMPLE
PS C:\Users\Smith\Documents> which Get-Command

Get-Command: Cmdlet in module Microsoft.PowerShell.Core

(Identifies type and source of command)
.EXAMPLE
PS C:\Users\Smith\Documents> which notepad

C:\WINDOWS\SYSTEM32
otepad.exe

(Indicates the full path of the executable)
#>

    param(
    [String]$name
    )

    $cmd = Get-Command $name
    $redirect = $null
    switch ($cmd.CommandType) {
       "Alias"          {"{0}: Alias for ({1})" -f $cmd.Name, (. { which cmd.Definition } ) }
       "Application"    { $cmd.Source }
       "Cmdlet"         {"{0}: {1} {2}" -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) {"in module {0}" -f $cmd.Source} else {"from unspecified source" } } ) }
       "Function"       {"{0}: {1} {2}" -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) {"in module {0}" -f $cmd.Source} else {"from unspecified source" } } ) }
       "Workflow"       {"{0}: {1} {2}" -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) {"in module {0}" -f $cmd.Source} else {"from unspecified source" } } ) }
       "ExternalScript" { $cmd.Source }
        default          { $cmd }
    }
}


用途:

1
2
3
4
5
6
7
8
9
10
11
function Which([string] $cmd) {
  $path = (($Env:Path).Split(";") | Select -uniq | Where { $_.Length } | Where { Test-Path $_ } | Get-ChildItem -filter $cmd).FullName
  if ($path) { $path.ToString() }
}

# Check if Chocolatey is installed
if (Which('cinst.bat')) {
  Write-Host"yes"
} else {
  Write-Host"no"
}

或此版本,调用原始的where命令。

此版本也工作得更好,因为它不限于BAT文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function which([string] $cmd) {
  $where = iex $(Join-Path $env:SystemRoot"System32\where.exe $cmd 2>&1")
  $first = $($where -split '[

]'
)
  if ($first.getType().BaseType.Name -eq 'Array') {
    $first = $first[0]
  }
  if (Test-Path $first) {
    $first
  }
}

# Check if Curl is installed
if (which('curl')) {
  echo 'yes'
} else {
  echo 'no'
}


推荐阅读

    共享盘挂载命令linux?

    共享盘挂载命令linux?,系统,信息,服务,设备,电脑,名字,情况,网络,命令,磁盘,l

    linux管理磁盘命令?

    linux管理磁盘命令?,系统,情况,管理,设备,报告,单位,数据,信息,命令,磁盘,lin

    linux三十个基本命令?

    linux三十个基本命令?,工作,地址,系统,信息,命令,管理,目录,单位,数据,生产,l

    linux查询ip命令?

    linux查询ip命令?,地址,网络,信息,设备,系统,电脑,终端,命令,中心,技术指标,l

    linux命令赋予权限?

    linux命令赋予权限?,系统,密码,档案,权限,数字,地址,用户,文件,命令,目录,详

    linux命令中的花括号?

    linux命令中的花括号?,系统,电脑,密码,命令,地址,代码,工作,管理,终端,文件,l

    夸虚机linux复制命令?

    夸虚机linux复制命令?,系统,工具,项目,电脑,位置,文件,命令,基本知识,源文

    linux命令行输入汉字?

    linux命令行输入汉字?,系统,软件,发行,管理,终端,命令,模式,编辑,文件,文本,L

    linux下编号的命令?

    linux下编号的命令?,工作,系统,信息,命令,状态,地址,工具,目录,软件,基础,在L

    linux命令解压怎么用?

    linux命令解压怎么用?,系统,命令,文件,软件,管理,标准,名称,工具,文件名,目

    linux过滤命令参数?

    linux过滤命令参数?,工具,数据,标准,地址,命令,设备,系统,信息,指令,文件,如

    linux解压常用命令?

    linux解压常用命令?,系统,工具,文件,电脑,命令,平台,手机,格式,目录,文件名,

    linux入侵网站命令?

    linux入侵网站命令?,工作,地址,信息,系统,数字,网站,名称,命令,网络,密码,lin

    conf命令linux?

    conf命令linux?,系统,网络,信息,命令,管理,路径,文件,模式,终端,光标,如何在L

    linux命令写在一起?

    linux命令写在一起?,系统,工作,信息,地址,命令,文件,目录,工具,内容,功能,lin

    linux开启终端命令行?

    linux开启终端命令行?,密码,系统,电脑,终端,首页,工具,软件,命令,权限,用户,l

    linuxvi选中命令?

    linuxvi选中命令?,工作,系统,位置,信息,正规,数字,首开,模式,命令,光标,linux

    linux怎么切换命令框?

    linux怎么切换命令框?,系统,密码,工具,电脑,首页,软件,地方,终端,界面,图形

    linux打开网络命令?

    linux打开网络命令?,网络,系统,地址,工作,信息,工具,管理,命令,基础,状态,Lin

    linux命令下载工具?

    linux命令下载工具?,工具,网络,代理,代码,简介,位置,系统,第一,下载工具,文