PowerShell to create Aliases for all your functions

I have a naming convention for all the function that I write, for example: JBMURPHY-AD-GetGroup. I wanted to create aliases for all my functions with my employers’s name, for example CompanyName-AD-GetGroup. I created the following function to “grep” all the function names and create an “Alias” file. This file would have all the set-alias commands that would be sourced on PowerShell start up. This function contains Regular Expressions (my weakness), which I borrowed from here.

FUNCTION JBMURPHY-CreateAliases{
PARAM($OLDNAME="JBMURPHY-",$NEWNAME="CompanyName-")
$tempFile = [System.IO.Path]::GetTempFileName()

Select-String -Pattern "function\s+(\w+-\w+-\w+)\b" *.ps1 | %{
$FUNCTIONS=$_.Matches | %{$_.groups[0].Value -Replace "Function ",""}
Write-host "set-alias $($FUNCTIONS -Replace $OLDNAME,$NEWNAME) $FUNCTIONS -Option AllScope"
Out-File $tempFile -encoding ascii -append -inputobject "set-alias $($FUNCTIONS -Replace $OLDNAME,$NEWNAME) $FUNCTIONS -Option AllScope"
}
move-item $tempFile "$($NEWNAME)ALIASES.ps1" -force
}

Comments are closed.