PoshCode Archive  Artifact [81e16ec696]

Artifact 81e16ec6968d1f6f8708ce791f6a3a77d7616d8cd64e5dbbc94fcfe08bf4f780:

  • File Get-AliasSuggestion.ps1 — part of check-in [3e2bfc94b8] at 2018-06-10 13:05:51 on branch trunk — From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes (user: Lee Holmes size: 1521)

# encoding: ascii
# api: powershell
# title: Get-AliasSuggestion.ps1
# description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
# version: 0.1
# type: function
# author: Lee Holmes
# license: CC0
# x-poshcode-id: 2146
# x-archived: 2016-12-02T04:43:47
# x-published: 2010-09-09T21:40:00
#
#
##############################################################################
##
## Get-AliasSuggestion
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Get an alias suggestion from the full text of the last command. Intended to
be added to your prompt function to help learn aliases for commands.

.EXAMPLE

Get-AliasSuggestion Remove-ItemProperty
Suggestion: An alias for Remove-ItemProperty is rp

#>

param(
    ## The full text of the last command
    $LastCommand
)

Set-StrictMode -Version Latest

$helpMatches = @()

## Find all of the commands in their last input
$tokens = [Management.Automation.PSParser]::Tokenize(
    $lastCommand, [ref] $null)
$commands = $tokens | Where-Object { $_.Type -eq "Command" }

## Go through each command
foreach($command in $commands)
{
    ## Get the alias suggestions
    foreach($alias in Get-Alias -Definition $command.Content)
    {
    $helpMatches += "Suggestion: An alias for " +
        "$($alias.Definition) is $($alias.Name)"
    }
}

$helpMatches