Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 347
Ken Bellows Avatar asked Nov 12 '10 15:11

Ken Bellows


People also ask

How do you pass arguments in PowerShell?

A default value will not work with a mandatory parameter. You can omit the =$true for advanced parameters of type boolean [Parameter(Mandatory)] . @Andrew First of all you have to change the type of the parameter to [string] . If you then want to pass a string as parameter you can use either ' or " .

How do I pass two arguments in PowerShell script?

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.

How do you create aliases in PowerShell?

PowerShell includes built-in aliases that are available in each PowerShell session. The Get-Alias cmdlet displays the aliases available in a PowerShell session. To create an alias, use the cmdlets Set-Alias or New-Alias . In PowerShell 6, to delete an alias, use the Remove-Alias cmdlet.


2 Answers

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.

like image 158
Mark Avatar answered Oct 02 '22 06:10

Mark


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

like image 42
Roman Kuzmin Avatar answered Oct 02 '22 06:10

Roman Kuzmin