Earlier this week I posted a function that you could incorporate into the PowerShell ISE to convert selected text to upper or lower case. I was challenged to take this a step further and come up with a way to convert aliases to commands. Which is exactly what I did.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
My initial approach, which I use and you may still find useful, was to take a selected word in the ISE and convert it to either and alias or a command. Naturally assuming that the selected word is one or the other. So I came up with a single function that can be used two ways depending on which direction you need to go.
[cc lang="PowerShell"]
Function Convert-AliasDefinition {
[cmdletBinding(DefaultParameterSetName="ToDefinition")]
Param(
[Parameter(Position=0,Mandatory=$True,HelpMessage="Enter a string to convert")]
[string]$Text,
[Parameter(ParameterSetName="ToAlias")]
[switch]$ToAlias,
[Parameter(ParameterSetName="ToDefinition")]
[switch]$ToDefinition
)
#make sure we are using the ISE
if ($host.name -match "ISE")
{
Try
{
#get alias if it exists otherwise throw an exception that
#will be caught
if ($ToAlias)
{
#get alias by definition and convert to name
$alias=get-alias -definition $Text -ErrorAction Stop
#there might be multiples so use the first one found
if ($alias -is [array])
{
$replace=$alias[0].name
}
else
{
$replace=$alias.name
}
}
else
{
#get alias by name and convert to definition
#if the text is ?, this is a special character so
#we'll just assume it is Where-Object
if ($Text -eq "?")
{
$Replace="Where-Object"
}
else
{
$alias=get-alias -name $Text -ErrorAction Stop
$replace=$alias.definition
}
} #Else ToDefinition
} #close Try
Catch
{
Write-Host "Nothing for for $text" -ForegroundColor Cyan
}
Finally
{
#make changes if an alias was found
If ($replace)
{
#Insert the replacment
$psise.currentfile.editor.insertText($replace)
}
}
} #if ISE
else
{
Write-Warning "You must be using the PowerShell ISE"
}
} #end function
[/cc]
The Convert-AliasDefinition function will only work in the PowerShell ISE and assumes you want to replace selected text. I would trust that most of the time you need to convert from an alias to the full command name since aliases in scripts are frowned upon. The function uses parameter sets and a set of switch parameters. The default is to convert an alias to command. The code is really quite simple: invoke the Get-Alias cmdlet for the alias and get the definition property.
[cc lang="PowerShell"]
$alias=get-alias -name $Text -ErrorAction Stop
$replace=$alias.definition
[/cc]
When converting a command to an alias, the function takes the opposite approach.
[cc lang="PowerShell"]
$alias=get-alias -definition $Text -ErrorAction Stop
#there might be multiples so use the first one found
if ($alias -is [array])
{
$replace=$alias[0].name
}
else
{
$replace=$alias.name
}
[/cc]
Because a command might have multiple aliases, I need to test if I got an array of aliases and if so I take the first one in the list. One problem I ran into was trying to accommodate the ? alias for Where-Object. This is kind of a special character in PowerShell so I used a brute force approach.
[cc lang="PowerShell"]
#if the text is ?, this is a special character so
#we'll just assume it is Where-Object
if ($Text -eq "?")
{
$Replace="Where-Object"
}
[/cc]
Once I have the replacement string, it is a simple matter of using the ISE object model to insert the text back into the file. Because the word was hightlighted to begin with this has the effect of updating just the text.
[cc lang="PowerShell"]
#make changes if an alias was found
If ($replace)
{
#Insert the replacment
$psise.currentfile.editor.insertText($replace)
}
[/cc]
To incorporate these changes into the ISE, I dot source the script file with the Convert-AliasDefiniton function in my ISE profile.
[cc lang="PowerShell"]
$psISE.CurrentPowerShellTab.AddOnsMenu.submenus.Add("Convert Selected to Alias",{Convert-AliasDefinition $psise.CurrentFile.Editor.SelectedText -ToAlias},$Null) | Out-Null
$psISE.CurrentPowerShellTab.AddOnsMenu.submenus.Add("Convert Selected to Command",{Convert-AliasDefinition $psise.CurrentFile.Editor.SelectedText -ToDefinition},$Null) | Out-Null
[/cc]
I didn't specify any keyboard shortcuts but you can if you wish. This will add two items to the Add-Ons menu. To use, I select an alias in my script, go to the menu, select Convert Selected to Command and presto chango the string is now a cmdlet name. This should work for any aliases defined and visible from your ISE session.
But, you may be asking, what about fixing an entire script? That is a little more complicated and I'll save that for tomorrow. In the mean time you can download Convert-AliasDefinition.
Neat ! Thank you.