-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathGet-CommandPath.ps1
69 lines (59 loc) · 2.01 KB
/
Get-CommandPath.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<#
.SYNOPSIS
Locates a command.
.DESCRIPTION
Returns the full path to an application found in a directory in $env:Path,
optionally with an extension from $env:PathExt.
.INPUTS
System.String of commands to get the location details of.
.OUTPUTS
System.String of location details for the specified commands that were found.
.FUNCTIONALITY
Command
.LINK
Get-Command
.EXAMPLE
Get-CommandPath.ps1 powershell
C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe
.EXAMPLE
Get-CommandPath.ps1 dotnet -FindAllInPath
C:\Program Files\dotnet\dotnet.exe
C:\Program Files (x86)\dotnet\dotnet.exe
#>
#Requires -Version 3
[CmdletBinding()][OutputType([string])] Param(
<#
The name of the executable program to look for in the $env:Path directories,
if the extension is omitted, $env:PathExt will be used to find one.
#>
[Parameter(Position=0,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
[Alias('Name','AN')][string[]] $ApplicationName,
# Indicates that every directory in the Path should be searched for the command.
[switch] $FindAllInPath
)
Process
{
if($FindAllInPath)
{
$files =
if((Split-Path $ApplicationName -Extension)) {$ApplicationName}
else {$env:PATHEXT.ToLower() -split ';' |ForEach-Object {[io.path]::ChangeExtension($ApplicationName,$_)}}
Write-Verbose "Searching Path for $($files -join ', ')"
foreach($p in $env:Path -split ';')
{
$files |
ForEach-Object {Join-Path $p $_} |
Where-Object {Test-Path $_ -Type Leaf}
}
}
else
{
foreach($cmd in Get-Command $ApplicationName)
{
if($cmd -is [Management.Automation.ApplicationInfo]) {$cmd.Path}
elseif($cmd -is [Management.Automation.ExternalScriptInfo]) {$cmd.Path}
elseif($cmd -is [Management.Automation.AliasInfo]) {$cmd.Definition}
else {Write-Error "$ApplicationName is $($cmd.GetType().FullName)"}
}
}
}