我如何询问PowerShell的位置?
例如,“notepad”,它根据当前路径返回运行notepad.exe的目录。
答案 0 :(得分:319)
我在PowerShell中开始自定义配置文件后,我创建的第一个别名是'which'。
New-Alias which get-command
要将此添加到您的个人资料,请输入以下内容:
"`nNew-Alias which get-command" | add-content $profile
最后一行开头的`n是确保它将作为新行开始。
答案 1 :(得分:139)
这是一个实际的* nix等价物,即它给出了* nix风格的输出。
Get-Command <your command> | Select-Object -ExpandProperty Definition
只需替换你想要的任何东西。
PS C:\> Get-Command notepad.exe | Select-Object -ExpandProperty Definition
C:\Windows\system32\notepad.exe
当您将其添加到配置文件时,您将需要使用函数而不是别名,因为您不能对管道使用别名:
function which($name)
{
Get-Command $name | Select-Object -ExpandProperty Definition
}
现在,当您重新加载个人资料时,您可以执行以下操作:
PS C:\> which notepad
C:\Windows\system32\notepad.exe
答案 2 :(得分:76)
我通常只输入:
gcm notepad
或
gcm note*
gcm是Get-Command的默认别名。
在我的系统上,gcm note *输出:
[27] » gcm note*
CommandType Name Definition
----------- ---- ----------
Application notepad.exe C:\WINDOWS\notepad.exe
Application notepad.exe C:\WINDOWS\system32\notepad.exe
Application Notepad2.exe C:\Utils\Notepad2.exe
Application Notepad2.ini C:\Utils\Notepad2.ini
您将获得与您要查找的内容相匹配的目录和命令。
答案 3 :(得分:36)
试试这个例子:
(Get-Command notepad.exe).Path
答案 4 :(得分:3)
这似乎做你想要的(我在http://huddledmasses.org/powershell-find-path/上找到了它):
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
答案 5 :(得分:3)
答案 6 :(得分:3)
我对哪个功能的提议:
function which($cmd) { get-command $cmd | % { $_.Path } }
PS C:\> which devcon
C:\local\code\bin\devcon.exe
答案 7 :(得分:2)
在Windows 2003或更高版本(或安装资源工具包的Windows 2000 / XP)上尝试where
命令。
答案 8 :(得分:2)
与Unix which
的快速匹配是
New-Alias which where.exe
但如果它们存在则返回多行,然后它变为
$(where.exe command | select -first 1)
答案 9 :(得分:1)
我喜欢Get-Command | Format-List
或更短,使用两者的别名,仅适用于powershell.exe
:
gcm powershell | fl
你可以找到这样的别名:
alias -definition Format-List
标签页完成功能适用于gcm
。
答案 10 :(得分:0)
使用:
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文件:
function which([string] $cmd) {
$where = iex $(Join-Path $env:SystemRoot "System32\where.exe $cmd 2>&1")
$first = $($where -split '[\r\n]')
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'
}
答案 11 :(得分:0)
我的PowerShell配置文件中有which
高级功能:
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\notepad.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 }
}
}
答案 12 :(得分:0)
如果您想要一个既接受管道输入又作为参数输入的命令,则应尝试以下操作:
TO_DATE(substr(date_column, 0,10),'YYYY-MM-DD')
将命令复制并粘贴到您的个人资料(function which($name) {
if ($name) { $input = $name }
Get-Command $input | Select-Object -ExpandProperty Path
}
)。
示例:
notepad $profile
答案 13 :(得分:0)
您可以从https://goprogram.co.uk/software/commands安装which
命令以及所有其他UNIX命令。