Yesterday I posted an article on how to convert a selected word to an alias or cmdlet. While I think there is still some value in this piecemeal approach. sometimes you want to make wholesale changes, such as when troubleshooting a script that someone else wrote that is full of cryptic aliases. I have a function you can integrate into the ISE that will convert all aliases in a block of selected text and convert them to their full cmdlet name equivalents.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
My function, ConvertTo-Definition, is designed to only work within the PowerShell ISE and assumes you want to convert all aliases to their command definitions. Personally, I don't see a reason for converting the other way in a script but you can add that functionality if you really need it.
[cc lang="Powershell"]
Function ConvertTo-Definition {
[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory=$True,HelpMessage="Enter a string to convert from an alias to a command")]
[string]$Text
)
#Verify we are in the ISE
if ($host.name -match "ISE")
{
Write-Verbose "Getting aliases"
$aliases=get-alias | where {$_.name -notmatch "\?|\%"}
foreach ($alias in $aliases) {
#match any alias on a word boundary that doesn't start with a $ or
#has a - before or after it.
if ($Text -match "(m?)(?<=\b)(?Convert-AllDefinition and let me know what you think.
Several people have commented about why I didn’t use the tokenizer. Well, my primary answer is because I’m not a developer and don’t think that way. Perhaps my result is a little more complicated but it doesn’t rely on any .NET programming knowledge. That said, someone sent me a code sample using the tokenizer and I’m working with it. I can see the advantages and ultimately you probably don’t really care how the conversion happens so I might post a revision when I find some time to thoroughly review and test.
Of course, if you write your script with full cmdlet names to begin with, this is all moot.