How can I write a PowerShell alias with arguments in the middle?

PowershellG++Alias

Powershell Problem Overview


I'm trying to set up a Windows PowerShell alias to run MinGW's g++ executable with certain parameters. However, these parameters need to come after the file name and other arguments. I don't want to go through the hassle of trying to set up a function and all of that. Is there a way to simply say something like:

alias mybuild="g++ {args} -lib1 -lib2 ..."

or something along those lines? I am not all that familiar with PowerShell, and I'm having a difficult time finding a solution. Anyone?

Powershell Solutions


Solution 1 - Powershell

You want to use a function, not an alias, as Roman mentioned. Something like this:

function mybuild { g++ $args -lib1 -lib2 ... }

To try this out, here's a simple example:

PS> function docmd { cmd /c $args there }
PS> docmd echo hello
hello there
PS> 

You might also want to put this in your profile in order to have it available whenever you run PowerShell. The name of your profile file is contained in $profile.

Solution 2 - Powershell

There is not such a way built-in. IMHO, a wrapper function is the best way to go so far. But I know that some workarounds were invented, for example:

https://web.archive.org/web/20120213013609/http://huddledmasses.org/powershell-power-user-tips-bash-style-alias-command

Solution 3 - Powershell

To build an function, store it as an alias, and persist the whole thing in your profile for later, use:

$g=[guid]::NewGuid();
echo "function G$g { COMMANDS }; New-Alias -Force ALIAS G$g">>$profile

where you have replaced ALIAS with the alias you want and COMMANDS with the command or string of commands to execute.

Of course, instead of doing that you can (and should!) make an alias for the above by:

echo 'function myAlias {
    $g=[guid]::NewGuid();
    $alias = $args[0]; $commands = $args[1]
    echo "function G$g { $commands }; New-Alias -Force $alias G$g">>$profile
}; New-Alias alias myAlias'>>$profile

Just in case your brain got turned inside out from all the recursion (aliasing of aliases, etc.), after pasting the second code block to your PowerShell (and restarting PowerShell), a simple example of using it is:

alias myEcho 'echo $args[0]'

or without args:

alias myLs 'ls D:\MyFolder'

Iff you don't have a profile yet

The above method will fail if you don't have a profile yet! In that case, use New-Item -type file -path $profile -force from this answer.

Solution 4 - Powershell

This is a sample function that will do different things based on how it was called:

Function Do-Something {
[CmdletBinding()] 
[Alias('DOIT')]
Param(
    [string] $option1,
    [string] $option2,
    [int] $option3)
#$MyInvocation|select *|FL
If ($MyInvocation.InvocationName -eq 'DOIT'){write-host "You told me to do it...so i did!" -ForegroundColor Yellow}
Else {Write-Host "you were boring and said do something..." -ForegroundColor Green}
}

Solution 5 - Powershell

Creating a 'filter' is also an option, a lighter alternative to functions. It processes each element in the pipeline, assigning it the $_ automatic variable. So, for instance:

filter test { Write-Warning "$args $_" }
'foo','bar' | test 'This is'

returns:

WARNING: This is foo
WARNING: This is bar

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKen BellowsView Question on Stackoverflow
Solution 1 - PowershellMarkView Answer on Stackoverflow
Solution 2 - PowershellRoman KuzminView Answer on Stackoverflow
Solution 3 - PowershellNH.View Answer on Stackoverflow
Solution 4 - PowershellJoshView Answer on Stackoverflow
Solution 5 - PowershellVopelView Answer on Stackoverflow