如何询问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 |
我通常只打:
或
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
| 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'
} |