Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

PowerShell ISE Alias to Command

Posted on April 7, 2011

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “PowerShell ISE Alias to Command”

  1. Vincent says:
    April 8, 2011 at 2:40 am

    Neat ! Thank you.

  2. Pingback: PowerShell ISE Convert All Aliases to Commands | The Lonely Administrator

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d