In interactive use, full cmdlet names (such as GetChildItem) are cumbersome and slow to type. Although aliases are much more efficient, it takes awhile to discover them. To learn aliases more easily, you can modify your prompt to remind you of the shorter version of any aliased commands that you use.
This involves two steps:
1. Add the program, GetAliasSuggestion.ps1, to your tools directory or other directory.
Example 19. GetAliasSuggestion.ps1
############################################################################## ## ## GetAliasSuggestion.ps1 ## ## Get an alias suggestion from the full text of the last command ## ## ie: ## ## PS > GetAliasSuggestion RemoveItemProperty ## Suggestion: An alias for RemoveItemProperty is rp ## ##############################################################################
param($lastCommand)
$helpMatches = @( )
## Get the alias suggestions foreach($alias in GetAlias) {
if($lastCommand match ("\b" + [System.Text.RegularExpressions.Regex]::Escape($alias.Definition) + "\b")) { $helpMatches += "Suggestion: An alias for $($alias.Definition) is $($alias.Name)" } }
$helpMatches
2. Prompt function in your profile. If you already have a prompt function, you only need to add the content from inside the prompt function of
A useful prompt to teach you aliases for common commands
function Prompt
{ ## Get the last item from the history $historyItem = GetHistory Count 1
## If there were any history items if($historyItem) {
## Get the training suggestion for that item $suggestions = @(GetAliasSuggestion $historyItem.CommandLine)
Example 110. A useful prompt to teach you aliases for common commands (continued)
## If there were any suggestions if($suggestions) {
## For each suggestion, write it to the screen foreach($aliasSuggestion in $suggestions) {
WriteHost "$aliasSuggestion" } WriteHost ""
} }
## Rest of prompt goes here "PS [$env:COMPUTERNAME] >" }