Équivalent de * Nix 'quelle' commande dans PowerShell?
-
09-06-2019 - |
Question
Comment demander à PowerShell où se trouve quelque chose?
Par exemple, quel bloc-notes " et il renvoie le répertoire à partir duquel le bloc-notes est exécuté en fonction des chemins actuels.
La solution
Le tout premier alias que j'ai créé une fois que j'ai commencé à personnaliser mon profil dans PowerShell était "quel".
New-Alias which get-command
Pour ajouter ceci à votre profil, tapez ceci:
"`nNew-Alias which get-command" | add-content $profile
Le `n au début de la dernière ligne est destiné à garantir qu’elle commence par une nouvelle ligne.
Autres conseils
Voici un équivalent * nix réel, c’est-à-dire qu’il donne une sortie de style * nix.
Get-Command <your command> | Select-Object -ExpandProperty Definition
Remplacez simplement par ce que vous cherchez.
PS C:\> Get-Command notepad.exe | Select-Object -ExpandProperty Definition
C:\Windows\system32\notepad.exe
Lorsque vous l'ajouterez à votre profil, vous voudrez utiliser une fonction plutôt qu'un alias car vous ne pouvez pas utiliser d'alias avec des tubes:
function which($name)
{
Get-Command $name | Select-Object -ExpandProperty Definition
}
Maintenant, lorsque vous rechargez votre profil, vous pouvez le faire:
PS C:\> which notepad
C:\Windows\system32\notepad.exe
D'habitude, je tape simplement:
gcm notepad
ou
gcm note*
gcm est l'alias par défaut de Get-Command.
Sur mon système, gcm note * génère:
[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
Vous obtenez le répertoire et la commande correspondant à ce que vous recherchez.
Essayez cet exemple:
(Get-Command notepad.exe).Path
Cela semble faire ce que vous voulez (je l’ai trouvé sur 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 Cela semble faire ce que vous voulez (je l’ai trouvé sur http://huddledmasses.org/ powershell-find-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
Cochez cette PowerShell .
Le code fourni à cet endroit suggère ceci:
($Env:Path).Split(";") | Get-ChildItem -filter notepad.exe
Ma proposition pour la fonction Quelle:
function which($cmd) { get-command $cmd | % { Ma proposition pour la fonction Quelle:
<*>.Path } }
PS C:\> which devcon
C:\local\code\bin\devcon.exe
Essayez la commande où
sous Windows 2003 ou une version ultérieure ( ou Windows 2000 / XP si vous avez installé un kit de ressources).
BTW, cela a reçu plus de réponses dans d'autres questions:
Correspondance rapide avec Unix
=
New-Alias which where.exe
Mais il retourne plusieurs lignes si elles existent, alors il devient
$(where.exe command | select -first 1)
J'aime Get-Command | Format-List
, ou plus court, utilisant des alias pour les deux et uniquement pour powershell.exe
:
gcm powershell | fl
Vous pouvez trouver des alias comme celui-ci:
alias -definition Format-List
La complétion par tabulation fonctionne avec gcm
.
J'ai ce quelle
fonction avancée dans mon profil 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 }
}
}
Utiliser:
function Which([string] $cmd) {
$path = (($Env:Path).Split(";") | Select -uniq | Where { Utiliser:
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'
}
Ou cette version, en appelant la commande where originale.
Cette version fonctionne également mieux, car elle ne se limite pas aux fichiers bat:
<*>.Length } | Where { Test-Path Utiliser:
<*>
Ou cette version, en appelant la commande where originale.
Cette version fonctionne également mieux, car elle ne se limite pas aux fichiers bat:
<*> } | 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"
}
Ou cette version, en appelant la commande where originale.
Cette version fonctionne également mieux, car elle ne se limite pas aux fichiers bat:
<*>