PowerShell이 어디에 있는지 물어 보려면 어떻게해야합니까?
예를 들어, "which notepad"는 현재 경로에 따라 notepad.exe가 실행되는 디렉토리를 반환합니다.
PowerShell에서 프로필을 사용자 정의하기 시작한 첫 번째 별칭은 'which'였습니다.
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
어떤 함수에 대한 나의 제안 :
function which($cmd) { get-command $cmd | % { $_.Path } }
PS C:\> which devcon
C:\local\code\bin\devcon.exe
이것은 당신이 원하는 것을하는 것처럼 보입니다 ( 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 Which .
제공된 코드는 다음을 제안합니다.
($Env:Path).Split(";") | Get-ChildItem -filter notepad.exe
Windows 2003 이상 (또는 Resource Kit를 설치 한 경우 Windows 2000/XP)에서 where
명령을 사용해보십시오.
BTW, 다른 질문에 더 많은 답변을 받았습니다.
유닉스 which
와 (과) 일치하는 더럽고 더러운
New-Alias which where.exe
그러나 여러 줄이 있으면 반환합니다.
$(where.exe command | select -first 1)
나는 Get-Command | Format-List
또는 더 짧은 것을 좋아합니다. 둘에 대한 별칭을 사용하고 powershell.exe
에 대해서만 사용하십시오.
gcm powershell | fl
다음과 같은 별칭을 찾을 수 있습니다.
alias -definition Format-List
탭 완성은 gcm
에서 작동합니다.
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 }
}
}
사용하다:
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 명령을 호출합니다.
이 버전은 박쥐 파일에만 국한되지 않기 때문에 더 잘 작동합니다.
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'
}