Skip to content

Instantly share code, notes, and snippets.

@DBremen
Last active August 16, 2022 00:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DBremen/9db6632423d673ff18f6 to your computer and use it in GitHub Desktop.
Save DBremen/9db6632423d673ff18f6 to your computer and use it in GitHub Desktop.
PowerShell function to expand aliases for ISE and files
function Expand-Alias{
[CmdletBinding(DefaultParameterSetName = 'Text')]
param(
[Parameter(Mandatory=$false, Position=0, ParameterSetName = 'Text')]
$code = $psISE.CurrentFile.Editor.Text,
[Parameter(Mandatory=$true, ParameterSetName = 'Path')]
[ValidateScript({
if (-not (Test-Path -PathType Leaf -LiteralPath $_ )) {
throw "Path '$_' does not exist. Please provide the path to an existing File."
}
$true
})]
$path
)
$htAliases = Get-Alias | Group-Object -Property Name -AsHashTable -AsString
$errors = $null
if ($PSCmdlet.ParameterSetName -eq 'Path'){
$code = Get-Content $path -Raw
}
[System.Management.Automation.PSParser]::Tokenize($code, [ref]$errors) |
Where-Object { $_.Type -eq 'Command' } |
Sort-Object { $_.Start } -Descending |
ForEach-Object{
$command = $_.Content
if ($htAliases.ContainsKey($command)){
$alias = $htAliases."$command" | Select-Object *
$definition = $alias.ResolvedCommandName
if ($PSCmdlet.ParameterSetName -eq 'Path'){
#replace the alias within the string
$code = $code.Remove($_.Start, $_.Content.Length).Insert( $_.Start, $definition)
}
else{
#replace the alias inside the ISE
$psISE.CurrentFile.Editor.Select($_.StartLine, $_.StartColumn, $_.EndLine, $_.EndColumn)
$psISE.CurrentFile.Editor.InsertText($definition)
}
}
}
if ($PSCmdlet.ParameterSetName -eq 'Path'){
Set-Content $path -Value $code
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment