PowerShell の *Nix 'what' コマンドに相当しますか?
-
09-06-2019 - |
質問
PowerShell に何かがどこにあるかを尋ねるにはどうすればよいですか?
たとえば、「どのメモ帳」と入力すると、現在のパスに従って notepad.exe が実行されているディレクトリが返されます。
解決
PowerShell でプロファイルのカスタマイズを開始してから最初に作成したエイリアスは「this」でした。
New-Alias which get-command
これをプロフィールに追加するには、次のように入力します。
"`nNew-Alias which get-command" | add-content $profile
最後の行の先頭にある `n は、確実に新しい行として開始されるようにするためのものです。
他のヒント
これは実際の *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
通常は次のように入力するだけです:
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
探しているものに一致するディレクトリとコマンドが得られます。
この例を試してみてください。
(Get-Command notepad.exe).Path
これはあなたが望むことを行うようです(私はそれを見つけました 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
これをチェックして PowerShell どれ.
そこに提供されているコードは、次のことを示唆しています。
($Env:Path).Split(";") | Get-ChildItem -filter notepad.exe
Which 関数に対する私の提案は次のとおりです。
function which($cmd) { get-command $cmd | % { $_.Path } }
PS C:\> which devcon
C:\local\code\bin\devcon.exe
試してみてください where
Windows 2003 以降 (リソース キットがインストールされている場合は Windows 2000/XP) のコマンド。
ところで、これは他の質問でさらに多くの回答を受け取りました。
Unix との簡単な組み合わせ which
は
New-Alias which where.exe
ただし、複数の行が存在する場合は返されるため、次のようになります
$(where.exe command | select -first 1)
好き Get-Command | Format-List
, 、またはそれより短く、2 つのエイリアスと のみを使用します。 powershell.exe
:
gcm powershell | fl
次のようなエイリアスを見つけることができます。
alias -definition Format-List
タブ補完は次のように機能します。 gcm
.
これを持っています which
PowerShell プロファイルの高度な機能:
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 }
}
}
使用:
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'
}