PowerShell 6 and ‘ls’

I’m one of those that always preferred to use the alias ls instead of dir for Get-ChildItem. In the beginning I mixed them up, but when I started using Linux more and more I liked that I could use the same command for the same thing in both PowerShell and bash.

Enter PowerShell 6, available to run on Linux and suddenly ls is not the same as ls anymore!

If you run both Windows and Linux, or at least both PowerShell for Windows and PowerShell 6 on Linux, you will see that using ls won’t give the same result anymore. On Windows it’s a built-in alias for Get-ChildItem of course, but since ls is the name of a native Linux command, that alias are not included in the Linux version of PowerShell 6 (understandably so).

Using ls on Linux just runs the native command as you would expect it to.

On Linux I tend to add the following alias to my .bashrc:

alias ll='ls -lAh --color=auto --group-directories-first'

So what if you would like to set a similar alias in PowerShell 6 on Linux? You can’t using the New/Set-Alias cmdlets, but there is a trick to get it to work I’m going to share with you. You just create a function instead. Simple, right? So the following in profile.ps1 will give you the same result:

function ll {ls $args -lAh --color=auto --group-directories-first}

This trick can of course be applied to any other alias you would like to make that includes parameters, both on Windows and Linux.

BTW. If you want the output of ll to be as similar as possible to the output of dir or Get-ChildItem on Linux, use the following function instead:

function ll {ls $args -l}

And if you find that you’d like the have a similar alias when on Windows, add the following function to your profile.ps1 on your Windows system:

function ll {ls $args -force}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s