Last active
April 15, 2023 02:07
-
-
Save JustinGrote/3eeec61472da1aa9f86a8f746eac905f to your computer and use it in GitHub Desktop.
Aliaser - Autogenerate shorthand aliases for powershell functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
param ( | |
#Provide a list of commands to alias. You can supply strings, modules, or commands from Get-Command. Only Noun-Verb commands will be processed. | |
[ValidateNotNullOrEmpty()][Parameter(ValueFromPipeline)][Object[]]$InputObject = (Get-Command '*-*' -CommandType Alias,Cmdlet,Function | Sort-Object Source,Name | Select-Object -ExpandProperty Name -Unique ), | |
[Switch]$PassThru | |
) | |
begin { | |
if ($PSEdition -eq 'Desktop') { | |
#Generated in Powershell 7 for Powershell 5.1 compatibility | |
$PSAliases = @{ | |
Add = 'a' | |
Approve = 'ap' | |
Assert = 'as' | |
Backup = 'ba' | |
Block = 'bl' | |
Build = 'bd' | |
Checkpoint = 'ch' | |
Clear = 'cl' | |
Close = 'cs' | |
Compare = 'cr' | |
Complete = 'cmp' | |
Compress = 'cm' | |
Confirm = 'cn' | |
Connect = 'cc' | |
Convert = 'cv' | |
ConvertFrom = 'cf' | |
ConvertTo = 'ct' | |
Copy = 'cp' | |
Debug = 'db' | |
Deny = 'dn' | |
Deploy = 'dp' | |
Disable = 'd' | |
Disconnect = 'dc' | |
Dismount = 'dm' | |
Edit = 'ed' | |
Enable = 'e' | |
Enter = 'et' | |
Exit = 'ex' | |
Expand = 'en' | |
Export = 'ep' | |
Find = 'fd' | |
Format = 'f' | |
Get = 'g' | |
Grant = 'gr' | |
Group = 'gp' | |
Hide = 'h' | |
Import = 'ip' | |
Initialize = 'in' | |
Install = 'is' | |
Invoke = 'i' | |
Join = 'j' | |
Limit = 'l' | |
Lock = 'lk' | |
Measure = 'ms' | |
Merge = 'mg' | |
Mount = 'mt' | |
Move = 'm' | |
New = 'n' | |
Open = 'op' | |
Optimize = 'om' | |
Out = 'o' | |
Ping = 'pi' | |
Pop = 'pop' | |
Protect = 'pt' | |
Publish = 'pb' | |
Push = 'pu' | |
Read = 'rd' | |
Receive = 'rc' | |
Redo = 're' | |
Register = 'rg' | |
Remove = 'r' | |
Rename = 'rn' | |
Repair = 'rp' | |
Request = 'rq' | |
Reset = 'rs' | |
Resize = 'rz' | |
Resolve = 'rv' | |
Restart = 'rt' | |
Restore = 'rr' | |
Resume = 'ru' | |
Revoke = 'rk' | |
Save = 'sv' | |
Search = 'sr' | |
Select = 'sc' | |
Send = 'sd' | |
Set = 's' | |
Show = 'sh' | |
Skip = 'sk' | |
Split = 'sl' | |
Start = 'sa' | |
Step = 'st' | |
Stop = 'sp' | |
Submit = 'sb' | |
Suspend = 'ss' | |
Switch = 'sw' | |
Sync = 'sy' | |
Test = 't' | |
Trace = 'tr' | |
Unblock = 'ul' | |
Undo = 'un' | |
Uninstall = 'us' | |
Unlock = 'uk' | |
Unprotect = 'up' | |
Unpublish = 'ub' | |
Unregister = 'ur' | |
Update = 'ud' | |
Use = 'u' | |
Wait = 'w' | |
Watch = 'wc' | |
Write = 'wr' | |
} | |
} else { | |
$PSAliases = @{} | |
(Get-Verb).foreach{$PSAliases[$PSItem.verb] = $PSItem.AliasPrefix} | |
} | |
} | |
process { | |
$commands = if ($InputObject -is [Management.Automation.PSModuleInfo]) { | |
$InputObject.exportedcommands | |
} else { | |
[String[]]$InputObject | |
} | |
#Noun-Verb Commands Only | |
$commands = $commands.where{$_ -match '-'} | Sort-Object -Unique | |
foreach ($commandItem in $commands) { | |
Write-Debug "START: $commandItem" | |
$existingAlias = (Get-Alias -Definition $commandItem -ErrorAction SilentlyContinue).name | |
if ($existingAlias -and $existingAlias -notmatch '-') { | |
Write-Verbose "Alias $existingAlias for $CommandItem already exists" | |
continue | |
} | |
$commandParts = $commandItem.split('-') | |
$verb = $PSAliases[$commandParts[0]] | |
if (-not $Verb) { | |
Write-Warning "$commandItem`: $($commandParts[0]) is not a recognized Powershell Verb. Will use full verb for alias" | |
$verb = $commandParts[0] | |
} | |
$noun = $commandParts[1] | |
[Char[]]$nounUppers = ($noun | Select-String -Pattern "[A-Z]" -CaseSensitive -AllMatches).matches.value | |
#Only the lowers after the last word | |
[Char[]]$nounLowers = ($noun | Select-String -Pattern "[a-z]+$" -CaseSensitive -AllMatches).matches.value | |
$aliasChars = $nounUppers + $nounLowers | |
[Text.StringBuilder]$Alias = $verb | |
$i=0 | |
$aliasResult = $null | |
do { | |
if (-not $aliasChars[$i]) { | |
Write-Warning "Unable to create an alias for $commandItem, all relevant abbrevations are taken" | |
break | |
} | |
[void]$Alias.append($aliasChars[$i]) | |
try { | |
$aliasResult = New-Alias -Name (([String]$Alias).tolower()) -Value $CommandItem -ErrorAction Stop -PassThru | |
Write-Debug "Attempt: $aliasResult for $CommandItem CREATED" | |
if ($PassThru) {$aliasResult} | |
} catch [Management.Automation.SessionStateException] { | |
if ($PSItem.FullyQualifiedErrorId -ne 'AliasAlreadyExists') {throw} | |
Write-Debug "Attempt: $(([String]$Alias).tolower()) for $CommandItem EXISTS" | |
$i++ | |
continue | |
} | |
} while ( | |
$null -eq $aliasResult | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment