I wish Get-ChildItem -force
to get executed when I type ll
and I have this in my profile.ps1
:
New-Alias -Name ll -Value Get-ChildItem -force
However, when I type ll
, I can see that the -force
argument is not being used. What am I doing wrong?
Edit: What I really wish to achieve is to show all files in a folder, even if they're hidden. And I wish to bind this to ll
.
You cannot do that with aliases. Aliases are really just different names for commands, they cannot include arguments.
What you can do, however, is, write a function instead of using an alias:
function ll {
Get-ChildItem -Force @args
}
You won't get tab completion for arguments in that case, though, as the function doesn't advertise any parameters (even though all parameters of Get-ChildItem
get passed through and work). You can solve that by effectively replicating all parameters of Get-ChildItem
for the function, akin to how PowerShell's own help
function is written (you can examine its source code via Get-Content Function:help
).
To add to Joey's excellent answer, this is how you can generate a proxy command for Get-ChildItem
(excluding provider-specific parameters):
# Gather CommandInfo object
$CommandInfo = Get-Command Get-ChildItem
# Generate metadata
$CommandMetadata = New-Object System.Management.Automation.CommandMetadata $CommandInfo
# Generate cmdlet binding attribute and param block
$CmdletBinding = [System.Management.Automation.ProxyCommand]::GetCmdletBindingAttribute($CommandMetadata)
$ParamBlock = [System.Management.Automation.ProxyCommand]::GetParamBloc($CommandMetadata)
# Register your function
$function:ll = [scriptblock]::Create(@'
{0}
param(
{1}
)
$PSBoundParameters['Force'] = $true
Get-ChildItem @PSBoundParameters
'@-f($CmdletBinding,$ParamBlock))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With