[Fixed]-Equivalent to Bash alias in PowerShell

26👍

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'

1👍

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

As for the stdout issue: What exactly are you experiencing?

👤Joey

1👍

I was struggling with this for a while and I’ve written this PowerShell module:

https://www.powershellgallery.com/packages/HackF5.ProfileAlias

https://github.com/hackf5/powershell-profile-alias

To get started you install it by:

Install-Module HackF5.ProfileAlias
Register-ProfileAliasInProfile

Then use it like:

Set-ProfileAlias dkfeed 'Enter-Docker feed_app_container' -Bash

I’ve been using it for a while myself and I’ve found it fairly useful.

(It only runs on PS 7.0 as I wrote it for myself).

Leave a comment