-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShow-ProfileFunction.ps1
More file actions
431 lines (363 loc) · 18 KB
/
Copy pathShow-ProfileFunction.ps1
File metadata and controls
431 lines (363 loc) · 18 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
function Show-ProfileFunction
{
<#
.SYNOPSIS
Shows a bulleted list of all available functions in the PowerShell profile Functions folder.
.DESCRIPTION
This function scans the Functions folder and extracts the SYNOPSIS from each PowerShell function file
to display a categorized list of available functions with their descriptions.
Functions are grouped by their category folder and sorted alphabetically within each category.
Helps users discover what functions are available in their profile.
Compatible with PowerShell Desktop 5.1+ on Windows, macOS, and Linux.
.PARAMETER Category
Filter the output to show only functions from the specified category or categories.
Accepts folder names (e.g., 'NetworkAndDns'), display names (e.g., 'Network And Dns'),
or short aliases (e.g., 'dns', 'network'). Case-insensitive. Supports tab completion.
Short aliases include: ad, dev, media, module, modules, network, net, dns, profile,
sysadmin, sys, admin, utils, util.
.EXAMPLE
PS > Show-ProfileFunction
Active Directory Functions:
- Invoke-GroupPolicyUpdate - Forces an immediate Group Policy update on Windows systems.
- Test-ADCredential - Test the username and password of Active Directory credentials.
- Test-ADUserLocked - Test if an Active Directory user account is locked out.
Developer Functions:
- Get-DotNetVersion - Gets the installed .NET Framework versions.
- Import-DotEnv - Loads environment variables from dotenv (.env) files.
- Remove-DotNetBuildArtifact - Removes bin and obj folders from .NET project directories.
...
Displays all available profile functions organized by category with brief descriptions.
.EXAMPLE
PS > Show-ProfileFunction -Category network
Network And Dns:
- Get-DnsRecord - Retrieves DNS records for a specified domain name.
- Test-Port - Tests network connectivity to a specified host and port.
...
Displays only the Network And Dns category functions using the short alias 'network'.
.EXAMPLE
PS > Show-ProfileFunction -Category dev, utils
Developer:
- Get-DotNetVersion - Gets the installed .NET Framework versions.
...
Utilities:
- Format-Byte - Formats a number of bytes into a human-readable string.
...
Displays functions from the Developer and Utilities categories.
.OUTPUTS
System.String
Formatted list of functions and descriptions
.NOTES
Author: Jon LaBelle
License: MIT
Source: https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/ProfileManagement/Show-ProfileFunction.ps1
.LINK
https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/ProfileManagement/Show-ProfileFunction.ps1
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')]
[CmdletBinding()]
[OutputType([String])]
param(
[Parameter(Position = 0)]
[ArgumentCompleter({
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$profileDir = if ($PROFILE) { Split-Path $PROFILE -Parent } else { $PSScriptRoot }
$functionsDir = Join-Path -Path $profileDir -ChildPath 'Functions'
if (-not (Test-Path $functionsDir)) { return }
$folders = Get-ChildItem -Path $functionsDir -Directory | Select-Object -ExpandProperty Name
# Custom short aliases for category names
$shortcuts = @{
'ActiveDirectory' = @('ad')
'Developer' = @('dev')
'MediaProcessing' = @('media')
'ModuleManagement' = @('module', 'modules')
'NetworkAndDns' = @('network', 'dns', 'net')
'ProfileManagement' = @('profile')
'SystemAdministration' = @('sysadmin', 'sys', 'admin')
'Utilities' = @('utils', 'util')
}
$completions = [System.Collections.Generic.List[string]]::new()
foreach ($folder in $folders)
{
$completions.Add($folder)
$spaced = ($folder -creplace '([A-Z])', ' $1').Trim()
if ($spaced -ne $folder) { $completions.Add($spaced) }
if ($shortcuts.ContainsKey($folder))
{
foreach ($s in $shortcuts[$folder]) { $completions.Add($s) }
}
}
$completions | Where-Object { $_ -like "$wordToComplete*" } | Sort-Object -Unique | ForEach-Object {
if ($_ -match '\s')
{
[System.Management.Automation.CompletionResult]::new("'$_'", $_, 'ParameterValue', $_)
}
else
{
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
})]
[string[]]$Category
)
begin
{
Write-Verbose 'Starting Show-ProfileFunction'
$skipProcessing = $false
# Get the Functions directory path relative to the profile script
$profilePath = $PROFILE
if (-not $profilePath)
{
$profilePath = $PSCommandPath
}
$functionsPath = Join-Path -Path (Split-Path $profilePath -Parent) -ChildPath 'Functions'
if (-not (Test-Path $functionsPath))
{
Write-Warning "Functions directory not found at: $functionsPath"
$skipProcessing = $true
return
}
Write-Verbose "Scanning Functions directory: $functionsPath"
# Custom category shortcuts for friendly short names
$categoryShortcuts = @{
'ActiveDirectory' = @('ad')
'Developer' = @('dev')
'MediaProcessing' = @('media')
'ModuleManagement' = @('module', 'modules')
'NetworkAndDns' = @('network', 'dns', 'net')
'ProfileManagement' = @('profile')
'SystemAdministration' = @('sysadmin', 'sys', 'admin')
'Utilities' = @('utils', 'util')
}
# Resolve user-specified category names to actual folder names
$resolvedCategories = @()
if ($Category)
{
$allFolders = Get-ChildItem -Path $functionsPath -Directory | Select-Object -ExpandProperty Name
foreach ($cat in $Category)
{
$catLower = $cat.ToLower()
$matched = $null
# Check custom shortcuts
foreach ($folder in $categoryShortcuts.Keys)
{
if ($categoryShortcuts[$folder] -contains $catLower)
{
$matched = $folder
break
}
}
# Check exact folder name (case-insensitive)
if (-not $matched)
{
$matched = $allFolders | Where-Object { $_.ToLower() -eq $catLower } | Select-Object -First 1
}
# Check spaced version (e.g., "Network And Dns" matches "NetworkAndDns")
if (-not $matched)
{
$matched = $allFolders | Where-Object {
$spaced = ($_ -creplace '([A-Z])', ' $1').Trim()
$spaced.ToLower() -eq $catLower
} | Select-Object -First 1
}
if ($matched)
{
$resolvedCategories += $matched
Write-Verbose "Resolved category '$cat' to folder '$matched'"
}
else
{
Write-Warning "Unknown category: '$cat'. Use tab completion to see available categories."
}
}
if ($resolvedCategories.Count -eq 0)
{
Write-Warning 'No valid categories specified. Run without -Category to see all.'
$skipProcessing = $true
return
}
}
}
process
{
if ($skipProcessing) { return }
try
{
# Get all PowerShell files in the Functions directory and subdirectories
$functionFiles = Get-ChildItem -Path $functionsPath -Filter '*.ps1' -File -Recurse
if (-not $functionFiles)
{
Write-Warning 'No PowerShell function files found in Functions directory'
return
}
# Group functions by category (parent folder)
$functionsByCategory = $functionFiles | Group-Object { $_.Directory.Name } | Sort-Object Name
# Filter by specified categories if provided
if ($resolvedCategories.Count -gt 0)
{
$functionsByCategory = $functionsByCategory | Where-Object { $resolvedCategories -contains $_.Name }
}
if (-not $functionsByCategory)
{
Write-Warning 'No functions found for the specified categories.'
return
}
$firstCategory = $true
foreach ($categoryGroup in $functionsByCategory)
{
# Add spaces to category name (e.g., "NetworkAndDns" -> "Network And Dns")
$categoryDisplay = $categoryGroup.Name -creplace '([A-Z])', ' $1'
$categoryDisplay = $categoryDisplay.Trim()
# Display category header with blank line before (except first)
if ($firstCategory)
{
Write-Host "`n${categoryDisplay}:" -ForegroundColor Cyan
$firstCategory = $false
}
else
{
Write-Host "`n${categoryDisplay}:" -ForegroundColor Cyan
}
# Sort functions within category
$sortedFiles = $categoryGroup.Group | Sort-Object Name
foreach ($file in $sortedFiles)
{
$functionName = ''
$synopsis = ''
$aliases = @()
try
{
# Use PowerShell's AST parser to reliably get function info
$tokens = $null
$errors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile($file.FullName, [ref]$tokens, [ref]$errors)
if ($errors.Count -gt 0)
{
Write-Verbose "Encountered $($errors.Count) parsing errors in $($file.Name)."
}
$functionAst = $ast.Find({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true)
if ($functionAst)
{
$functionName = $functionAst.Name
$helpContent = $functionAst.GetHelpContent()
# Fallback: If GetHelpContent() returns null or empty, manually parse the help block
if (-not $helpContent -or [string]::IsNullOrWhiteSpace($helpContent.Synopsis))
{
Write-Verbose "GetHelpContent() failed for $($file.Name), using manual parsing"
$fileContent = Get-Content $file.FullName -Raw
# Extract .SYNOPSIS using regex - handle multi-line content
if ($fileContent -match '(?s)\.SYNOPSIS\s+(.+?)(?=\r?\n\s*\.)')
{
$synopsisText = $matches[1].Trim()
# Create a minimal help content object
$helpContent = [PSCustomObject]@{
Synopsis = $synopsisText
Description = $null
}
}
}
# Extract aliases from Set-Alias commands in the file
$setAliasCalls = $ast.FindAll({
$args[0] -is [System.Management.Automation.Language.CommandAst] -and
$args[0].GetCommandName() -eq 'Set-Alias'
}, $true)
foreach ($aliasCall in $setAliasCalls)
{
# Extract the -Name parameter value
$nameParam = $aliasCall.CommandElements | Where-Object {
$_ -is [System.Management.Automation.Language.CommandParameterAst] -and
$_.ParameterName -eq 'Name'
}
if ($nameParam)
{
$nameIndex = $aliasCall.CommandElements.IndexOf($nameParam)
if ($nameIndex -ge 0 -and ($nameIndex + 1) -lt $aliasCall.CommandElements.Count)
{
$aliasValue = $aliasCall.CommandElements[$nameIndex + 1]
if ($aliasValue -is [System.Management.Automation.Language.StringConstantExpressionAst])
{
$aliases += $aliasValue.Value
}
}
}
}
if ($helpContent)
{
# Extract synopsis, fallback to description
$synopsisText = if (-not [string]::IsNullOrWhiteSpace($helpContent.Synopsis))
{
$helpContent.Synopsis
}
elseif (-not [string]::IsNullOrWhiteSpace($helpContent.Description))
{
$helpContent.Description
}
else
{
'No description available'
}
# Normalize newlines and join multi-line text into a single line
$synopsis = ($synopsisText -split '\r?\n' | ForEach-Object { $_.Trim() }) -join ' '
}
else
{
$synopsis = 'No description available'
}
}
else
{
# Fallback for files that might not contain a standard function definition
$functionName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
$synopsis = 'Could not parse function definition'
}
# Truncate if too long to keep single line
if ($synopsis.Length -gt 80)
{
$synopsis = $synopsis.Substring(0, 77) + '...'
}
}
catch
{
Write-Verbose "Error parsing file $($file.Name): $($_.Exception.Message)"
$functionName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
$synopsis = 'Unable to read description'
}
# Format and display the function with description
Write-Host ' - ' -ForegroundColor Yellow -NoNewline
Write-Host $functionName -ForegroundColor Green -NoNewline
# Display aliases if available
if ($aliases.Count -gt 0)
{
Write-Host ' (' -ForegroundColor Gray -NoNewline
Write-Host ($aliases -join ', ') -ForegroundColor DarkGray -NoNewline
# Write-Host '*' -ForegroundColor Gray -NoNewline
Write-Host ')' -ForegroundColor Gray -NoNewline
}
Write-Host ' - ' -ForegroundColor Yellow -NoNewline
Write-Host $synopsis -ForegroundColor White
}
}
# Display summary statistics
$displayedCount = ($functionsByCategory | ForEach-Object { $_.Group.Count } | Measure-Object -Sum).Sum
$categoryCount = @($functionsByCategory).Count
Write-Host "`nTotal: " -ForegroundColor Cyan -NoNewline
Write-Host "$displayedCount functions " -ForegroundColor White -NoNewline
Write-Host 'across ' -ForegroundColor Cyan -NoNewline
Write-Host "$categoryCount categories" -ForegroundColor White
Write-Host "`nAliases shown in parentheses are only created if they don't already exist in the environment." -ForegroundColor DarkGray
# Add helpful footer
Write-Host "`nFor full details about any function, use: " -ForegroundColor Gray -NoNewline
Write-Host 'Get-Help <Function-Name>' -ForegroundColor White
Write-Host 'Example: ' -ForegroundColor Gray -NoNewline
Write-Host 'Get-Help Test-Port -Full' -ForegroundColor White
}
catch
{
Write-Error "Error processing Functions directory: $($_.Exception.Message)"
throw $_
}
}
end
{
Write-Verbose 'Show-ProfileFunction completed'
}
}