Equivalent to Bash alias in PowerShell

PowerShell newbie question:

I would 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 this regard, I found this very difficult.

-PowerShell aliases only work with PowerShell commands + function calls

- There is no clear way to allow an unlimited number of arguments when calling a PowerShell function

-PowerShell seems to block stdout


It is worth noting that I tried the solution proposed here: http://huddledmasses.org/powershell-power-user-tips-bash-style-alias-command/

And received the following syntax error while loading 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 
+7
django powershell
source share
2 answers

PowerShell aliases can be used for any "command", including EXE:

 new-alias n notepad.exe 

Update : as mentioned in the comments, PowerShell aliases do not accept arguments. These are simple β€œaliases” for the team name and nothing more. To get what you need (I think, since I am not familiar with Bash aliases), try the following:

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

It uses a PowerShell 2.0 function called the splatting argument. You can apply @ to a variable name that refers to either an array or a hash table. In this case, we apply it to a variable called args , which contains all the unnamed parameters.

If you need a really general way to create alias functions that take parameters, try the following:

 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' 

Regarding the issue with Joel's approach, I suspect it has which with the Get-Command alias. Try replacing which with Get-Command .

+8
source share

Functions can have arbitrarily many arguments. You just need to use $args to access them.

Regarding the stdout problem: what exactly are you experiencing?

+1
source share

All Articles