Expand-Alias for PowerShell Integrated Scripting Environment - January 3rd, 2009
Get your PowerShell code ready for production by expanding your shorthand alias syntax.
Use the shorthand approach to knock out your scripts. Then expand them. Makes your code more readable and maintainable in the long run.
Press Ctrl+Shift+E
How To
Add this code to your Microsoft.PowerShellISE_profile.ps1
Function Expand-Alias {
$content=$psise.CurrentOpenedFile.Editor.text
[System.Management.Automation.PsParser]::Tokenize($content, [ref] $null) |
Where { $_.Type -eq ‘Command’} |
Sort StartLine, StartColumn -Desc |
ForEach {
if($_.Content -eq ‘?’) {
$result = Get-Command ‘`?’ -CommandType Alias
} else {
$result = Get-Command $_.Content -CommandType Alias -ErrorAction SilentlyContinue
}
if($result)
{
$psise.CurrentOpenedFile.Editor.Select($_.StartLine,$_.StartColumn,$_.EndLine,$_.EndColumn)
$psise.CurrentOpenedFile.Editor.InsertText($result.Definition)
}
}
}
if( -Not ($psISE.CustomMenu.Submenus | ?{$_.DisplayName -eq ‘Expand Alias’}) ) {
$null = $psISE.CustomMenu.Submenus.Add("Expand Alias", {Expand-Alias}, ‘Ctrl+Shift+E’)
}
