-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-WhichCommand.ps1
More file actions
283 lines (251 loc) · 9.88 KB
/
Copy pathGet-WhichCommand.ps1
File metadata and controls
283 lines (251 loc) · 9.88 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
function Get-WhichCommand
{
<#
.SYNOPSIS
Locates a command and displays its location or type (mimics POSIX which).
.DESCRIPTION
Cross-platform function that mimics the behavior of the POSIX 'which' command.
Searches for commands in the following order:
1. PowerShell aliases
2. PowerShell functions
3. PowerShell cmdlets
4. External executables in PATH
Returns the full path for executables or the definition/type for PowerShell commands.
Supports multiple command names and pipeline input.
Aliases:
The 'which' alias is created only if it doesn't already exist in the current environment.
.PARAMETER Name
The name of the command(s) to locate. Accepts multiple values and pipeline input.
.PARAMETER All
Display all matches instead of just the first one found.
Includes the CommandType, Name, Definition, and Source in the output.
By default, only the first match is returned (mimics POSIX 'which' behavior).
.EXAMPLE
PS > Get-WhichCommand git
C:\Program Files\Git\cmd\git.exe
Locates the git executable and returns its full path (POSIX which behavior).
.EXAMPLE
PS > Get-WhichCommand ls
ls -> Get-ChildItem
Shows that 'ls' is an alias for Get-ChildItem.
.EXAMPLE
PS > 'git', 'pwsh', 'Get-Process' | Get-WhichCommand
/opt/homebrew/bin/git
/usr/local/microsoft/powershell/7/pwsh
Microsoft.PowerShell.Management
Locates multiple commands via pipeline input.
.EXAMPLE
PS > Get-WhichCommand 'pytho*' -All
CommandType Name Definition Source
----------- ---- ---------- ------
Application python3 /usr/bin/python3 /usr/bin/python3
Application python3.13 /opt/homebrew/bin/python3.13 /opt/homebrew/bin/python3.13
Application python3.13-config /opt/homebrew/bin/python3.13-config /opt/homebrew/bin/python3.13-config
Shows all executables matching 'pytho*' pattern found in PATH.
.OUTPUTS
String with command path or definition.
.NOTES
Author: Jon LaBelle
License: MIT
Source: https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/Utilities/Get-WhichCommand.ps1
.LINK
https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/Utilities/Get-WhichCommand.ps1
#>
[CmdletBinding()]
[OutputType([String])]
param(
[Parameter(Mandatory, ValueFromPipeline, Position = 0)]
[ValidateNotNullOrEmpty()]
[String[]]$Name,
[Parameter()]
[Alias('Detailed')]
[Switch]$All
)
begin
{
Write-Verbose 'Starting Get-WhichCommand'
}
process
{
foreach ($cmdName in $Name)
{
Write-Verbose "Searching for command: $cmdName"
$found = $false
try
{
# Get all matching commands
$commands = Get-Command -Name $cmdName -ErrorAction SilentlyContinue
if (-not $commands)
{
Write-Verbose "Command not found: $cmdName"
Write-Warning "Command not found: $cmdName"
continue
}
# If not using -All, take only the first match (default behavior)
# This mimics POSIX 'which' which returns the first executable found in PATH
if (-not $All)
{
$commands = @($commands)[0]
}
foreach ($cmd in $commands)
{
$found = $true
switch ($cmd.CommandType)
{
'Alias'
{
$resolvedCommand = $cmd.ResolvedCommand
if ($All)
{
[PSCustomObject]@{
CommandType = 'Alias'
Name = $cmd.Name
Definition = if ($resolvedCommand) { "$($cmd.Name) -> $($resolvedCommand.Name)" } else { "$($cmd.Name) -> $($cmd.Definition)" }
Source = $cmd.Source
}
}
else
{
if ($resolvedCommand)
{
"$($cmd.Name) -> $($resolvedCommand.Name)"
}
else
{
"$($cmd.Name) -> $($cmd.Definition)"
}
}
}
'Function'
{
if ($All)
{
[PSCustomObject]@{
CommandType = 'Function'
Name = $cmd.Name
Definition = if ($cmd.ScriptBlock.File) { $cmd.ScriptBlock.File } else { '<ScriptBlock>' }
Source = $cmd.Source
}
}
else
{
if ($cmd.ScriptBlock.File)
{
$cmd.ScriptBlock.File
}
else
{
"$($cmd.Name) (Function)"
}
}
}
'Cmdlet'
{
if ($All)
{
[PSCustomObject]@{
CommandType = 'Cmdlet'
Name = $cmd.Name
Definition = $cmd.Source
Module = $cmd.ModuleName
}
}
else
{
if ($cmd.Source)
{
$cmd.Source
}
else
{
"$($cmd.Name) (Cmdlet from $($cmd.ModuleName))"
}
}
}
'Application'
{
if ($All)
{
[PSCustomObject]@{
CommandType = 'Application'
Name = $cmd.Name
Definition = $cmd.Source
Source = $cmd.Source
}
}
else
{
$cmd.Source
}
}
'ExternalScript'
{
if ($All)
{
[PSCustomObject]@{
CommandType = 'ExternalScript'
Name = $cmd.Name
Definition = $cmd.Source
Source = $cmd.Source
}
}
else
{
$cmd.Source
}
}
default
{
if ($All)
{
[PSCustomObject]@{
CommandType = $cmd.CommandType
Name = $cmd.Name
Definition = $cmd.Definition
Source = $cmd.Source
}
}
else
{
if ($cmd.Source)
{
$cmd.Source
}
else
{
$cmd.Definition
}
}
}
}
}
if (-not $found)
{
Write-Verbose "No results for: $cmdName"
}
}
catch
{
Write-Verbose "Error processing command '$cmdName': $($_.Exception.Message)"
throw $_
}
}
}
end
{
Write-Verbose 'Get-WhichCommand completed'
}
}
# Create 'which' alias only if the native which command doesn't exist
if (-not (Get-Command -Name 'which' -CommandType Application -ErrorAction SilentlyContinue))
{
try
{
Write-Verbose "Creating 'which' alias for Get-WhichCommand"
Set-Alias -Name 'which' -Value 'Get-WhichCommand' -Force -ErrorAction Stop
}
catch
{
Write-Warning "Get-WhichCommand: Could not create 'which' alias: $($_.Exception.Message)"
}
}