Frage

Wie frage ich PowerShell, wo sich etwas befindet?

Zum Beispiel „Welcher Notepad“ und es wird das Verzeichnis zurückgegeben, in dem die Datei „notepad.exe“ gemäß den aktuellen Pfaden ausgeführt wird.

War es hilfreich?

Lösung

Der allererste Alias, den ich erstellt habe, als ich mit der Anpassung meines Profils in PowerShell begann, war „which“.

New-Alias which get-command

Um dies zu Ihrem Profil hinzuzufügen, geben Sie Folgendes ein:

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

Das `n am Anfang der letzten Zeile soll sicherstellen, dass sie als neue Zeile beginnt.

Andere Tipps

Hier ist ein tatsächliches * nix Äquivalent, das heißt es ergibt * nix-style-Ausgang.

Get-Command <your command> | Select-Object -ExpandProperty Definition

Ersetzen Sie einfach mit dem, was Sie suchen.

PS C:\> Get-Command notepad.exe | Select-Object -ExpandProperty Definition
C:\Windows\system32\notepad.exe

Wenn Sie es zu Ihrem Profil hinzufügen, werden Sie eine Funktion verwenden, anstatt ein Alias, weil Sie keine Aliase mit Rohren verwenden können:

function which($name)
{
    Get-Command $name | Select-Object -ExpandProperty Definition
}

Nun, wenn Sie Ihr Profil neu zu laden Sie können dies tun:

PS C:\> which notepad
C:\Windows\system32\notepad.exe

Normalerweise tippe ich einfach:

gcm notepad

oder

gcm note*

gcm ist der Standardalias für Get-Command.

Auf meinem System gibt gcm note* Folgendes aus:

[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

Sie erhalten das Verzeichnis und den Befehl, der Ihren Suchanfragen entspricht.

Versuchen Sie dieses Beispiel:

(Get-Command notepad.exe).Path

Dies scheint zu tun, was Sie wollen (ich fand es auf 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

Überprüfen Sie diese Powershell Welche .

Der Code vorausgesetzt, es schlägt vor, diese:

($Env:Path).Split(";") | Get-ChildItem -filter notepad.exe

Mein Vorschlag für die, welche Funktion:

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

PS C:\> which devcon

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

Versuchen Sie, den where Befehl unter Windows 2003 oder höher (oder Windows 2000 / XP, wenn Sie habe ein Resource Kit installiert ist).

BTW, das erhalten mehr Antworten in anderen Fragen:

Gibt es ein Äquivalent von 'denen' unter Windows?

Powershell entspricht Unix which Befehl?

Ein schnelles und unsauberes Spiel zu Unix which ist

New-Alias which where.exe

Aber gibt es mehrere Zeilen, wenn sie vorhanden sind, so dann wird es

$(where.exe command | select -first 1)

Ich mag Get-Command | Format-List, oder kürzer, Aliase für die zwei verwenden und nur für powershell.exe:

gcm powershell | fl

Sie können Aliase wie diese finden:

alias -definition Format-List

Tabulatorvervollständigung arbeitet mit gcm.

Ich habe diese which erweiterte Funktion in meinem Powershell-Profil:

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

Verwendung:

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

oder diese Version, den Aufruf der ursprünglichen wo Befehl.

Diese Version funktioniert auch besser, weil es nicht zu schlagen Dateien beschränkt ist:

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'
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top