Antony Thomas
Antony Thomas

Reputation: 3696

Setting an alias with attributes in PowerShell

I wanted to set an alias for listing files in the directory, but Set-Alias -name lf -value ls -file does not seem to work. I intend to use this the Unix alias way.

Upvotes: 2

Views: 861

Answers (3)

Edward Liang
Edward Liang

Reputation: 100

Append to the answer from @mike-z .

You can put the function definition into the PowerShell profile so that you can reuse it opening shell again.

test-path $profile
// Ensure it doesn't exists before creating the profile!
new-item -path $profile -itemtype file -force
notepad $profile

Simply put the code into the file:

function lf { ls -file @args }

You can check the details from official documentation.

Upvotes: 0

Nick
Nick

Reputation: 4362

Example 5 from Get-Help Set-Alias -Full is what you want:

Function lsfile {Get-Childitem -file}
Set-Alias lf lsfile

Upvotes: 0

Mike Zboray
Mike Zboray

Reputation: 40838

An alias can't do that. From the help for Set-Alias:

You can create an alias for a cmdlet, but you cannot create an alias for a command that consists of a cmdlet and its parameters.

However, using a technique called "splatting", a function can do it easily:

function lf {
  ls -file @args
}

For more information, see help about_splatting.

Upvotes: 9

Related Questions