PowerShell이 어디에 있는지 어떻게 물어 보나요?
예를 들어, “어떤 메모장”이고 현재 경로에 따라 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
답변
유닉스와의 빠르고 더러워진 일치 which
는
New-Alias which where.exe
그러나 여러 줄이 있으면 반환합니다.
function which {where.exe command | select -first 1}
답변
이것은 당신이 원하는 것을하는 것처럼 보입니다 ( 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
