Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent to Bash alias in PowerShell

A newbie PowerShell question:

I'd like to make an alias in PowerShell exactly equivalent to this Bash alias:

alias django-admin-jy="jython /path/to/jython-dev/dist/bin/django-admin.py"

In tinkering with it so far, I've found this to be very difficult.

-PowerShell aliases only work with PowerShell commands + function calls

-No clear way to allow for an unlimited number of arguments on a PowerShell function call

-PowerShell seems to block stdout


It's worth noting that I've tried the solution put forth here: http://huddledmasses.org/powershell-power-user-tips-bash-style-alias-command/

And have gotten the following syntax-related error on loading up PowerShell:


The term 'which' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spell
ing of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\Dan\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:9 char:27

+             $cmd = @(which <<<<  $_.Content)[0]
    + CategoryInfo          : ObjectNotFound: (which:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
like image 409
RightFullRudder Avatar asked Mar 18 '10 07:03

RightFullRudder


People also ask

What is the alias command in PowerShell?

An alias is an alternate name for a cmdlet, function, executable file, including scripts. PowerShell includes a set of built-in aliases. You can add your own aliases to the current session and to your PowerShell profile.

Can I create alias 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.

What is the equivalent of .bashrc in PowerShell?

As pointed out by Dai in the comment section, Powershell Profiles sound like the closest equivalent to the . bashrc file. A PowerShell profile is a script that runs when PowerShell starts. You can use the profile as a logon script to customize the environment.

Can you use alias in shell script?

Your alias is well defined, but you have to store it in ~/. bashrc , not in a shell script. Add it to that file and then source it with . . bashrc - it will load the file so that alias will be possible to use.


1 Answers

PowerShell aliases do not allow for arguments.
They can only refer to a command name, which can be the name of a cmdlet or a function, or the name / path of a script or executable.

To get what you are after, you need to define a function:

function django-admin-jy {
    jython.exe /path/to/jython-dev/dist/bin/django-admin.py @args
}

This uses a feature available since PowerShell 2.0 called argument splatting: you can apply @ to a variable name that references either an array or a hashtable.
In this case, we apply it to the automatic variable named args, which contains all arguments (that weren't bound to explicitly declared parameters - see about_Functions).

If you want a truly generic way to create aliases functions that take parameters, try this:

function New-BashStyleAlias([string]$name, [string]$command)
{
    $sb = [scriptblock]::Create($command)
    New-Item "Function:\global:$name" -Value $sb | Out-Null
}

New-BashStyleAlias django-admin-jy 'jython.exe /path/to/jython-dev/dist/bin/django-admin.py @args'
like image 133
Keith Hill Avatar answered Sep 23 '22 13:09

Keith Hill