문제

나는 어떻게 요청 PowerShell 는 뭔가?

예를 들어,"는 메모장을"그리고 그것은 반환하는 디렉토리 notepad.exe 에서 실행에 따라 현재 경로입니다.

도움이 되었습니까?

해결책

첫 번째 별칭을 내가 만든 작업을 시작하면서 사용자 지정 내에 프로필 PowerShell'는'.

New-Alias which get-command

추가 이 프로필,형식이다:

"`nNew-Alias which get-command" | add-content $profile

`N 의 시작에서 마지막 줄은 그것을 지키는 것으로 시작하고 새로운 라인입니다.

다른 팁

여기에 실제*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 본에 대한 별칭을 얻-명령입니다.

시스템에,gcm 참고*출력:

[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

내 건의안에 대한있는 기능:

function which($cmd) { get-command $cmd | % { $_.Path } }

PS C:\> which devcon

C:\local\code\bin\devcon.exe

where 에서 명령 윈도우 2003 이상(또는 윈도우 2000/XP 에 설치한 경우는 리소스 키트).

BTW,이 더 받아답에 다른 질문:

은 거기에 해당하는'윈도우에서?

PowerShell 해당하는 Unix which 명령?

빠르고 더러운 경기를 Unix 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.

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"
}

또한 이전 호출,원래는 명령입니다.

이 버전은 또한 더 나은 작품이기 때문에,그것은 제한되지 않을 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'
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top