Any way to remap ls from Get-ChildItem to another command in PowerShell?

265 Views Asked by At

Trying to replace the default PowerShell Get-ChildItem command (mapped to ls by default in PowerShell 7) with eza, however I cannot seem to find anything on how to do so

  • Using New-Alias results in alias is not allowed
Line |
  15 |  New-Alias -Name ls -Value eza.exe # Eza map to ls
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The alias is not allowed, because an alias with the name 'ls' already exists.
  • Using the following function does not work and it just calls Get-ChildItem again (although if i, for example, switch ls to la it does work so i don't think the function itself is incorrect)
function ls {
    [string]$Path # path for ls
    eza.exe -lab --group-directories-first --git --icons $Path
}

1

There are 1 best solutions below

0
Santiago Squarzon On

You need to use -Force to override an existing alias:

New-Alias -Name ls -Value eza.exe -Force

Or even simpler, use Set-Alias or assign a value using the alias: modifier:

Set-Alias ls -Value eza.exe
# or
$alias:ls = 'eza.exe'

As for defining a function with the same name, note that aliases in PowerShell are at the top of the Command Precedence, meaning, a function with the same name will not override them.

You can however, name your function differently and decorate it with the Alias attribute, then you're good (note, you're missing a param block too):

function My-Func {
    [alias('ls')]
    param(
        [string]$Path # path for ls
    )

    eza.exe -lab --group-directories-first --git --icons $Path
}