You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support user defined parameter aliases, e.g. -F to -Force for Remove-Item #2199
Having Remove-Item -F work just like Remove-Item -Force seems like a good idea.
There is a small concern - Remove-Item is a cmdlet that is implemented by providers (like the file system, registery, environment, etc) and providers can add dynamic parameters. Providers that add dynamic parameters aren't all that common, but there is a risk that a provider adds a dynamic parameter with an identical alias -F which would be masked by this change.
In an ideal world, this wouldn't break scripts because they should be using full parameter names, but we don't live in that ideal world.
It feels like an unlikely breaking change, but we should have data to confirm that assumption before considering this enhancement.
Belleve (@be5invis) - in theory, sure, but I think that would be difficult to implement correctly.
Dynamic parameters don't support adding an alias to an existing parameter and we couldn't move -Force to be dynamic for a specific set of providers because the parameter must exist for all providers.
I'm proposing a way to implement this... Allow specifying the precedence of a parameter when ambiguity happens. E.g. [Parameter(Precedence = 1)][switch]$Force, [Parameter(Precedence = 0)][string]$Filter, then -F, if not otherwise resolved, would be -Force.
For backward compatibility, Precedence should be System.Nullable<System.Int32> or System.Nullable<System.UInt32>. Resolution step regarding precedence (of course full name and aliases should be used to resolve parameters before we come to this step) should be:
If no parameter candidate has Precedence.HasValue, the resolution fails;
Collect all parameter candidates with Precedence.HasValue that has the largest Precedence.Value, say this is collection A;
If the size of A is 1, the parameter resolves to the only element in A;
Otherwise, the resolution fails.
This won't break current providers that adds a dynamic parameter / an alias of a dynamic parameter -F, and is consistent in all providers -- You don't lose -F as an alias for -Force magically because you are not using built-in providers, you lose it because -F resolves before it reaches -Force and -Filter.
Using aliases degrades readability of scripts.
Using Foreach-Object better using foreach.
Using Where-Object better using where.
Using -Force better using -F.
In reply to Ilya (@iSazonov) : If you read carefully enough, you would find that the proposed alias -F for -Force is intended to be used interactively, shortening the type for a often-used switch.
I'd vote against building in a shorthand aliases for parameters (such as -F for -Force) because it could lead to accidents.. It's pretty well known that you can abbreviate any parameter using the minimum number of characters required to unambiguously identify that parameter. If -F is explicitly aliased to -Force, and someone forgets about the conflict between -Force and -Filter because there aren't multiple parameters that start with -F on every command that has a -Force parameter, suddenly they may invoke a command with -Force when they really meant to invoke it with -Filter.
A better solution for this problem would be to unlock parameter aliases in PowerShell (they currently cannot be modified by end users), allowing PowerShell users to add parameter aliases that they want to use in their interactive work to their profile, much like they can add command aliases today. Then if someone specifically wants -F to be an alias for -Force in Remove-Item for ad-hoc work, they can make that decision and change it for themselves without forcing a potentially unwanted change on the rest of the community.
In this particular scenario, having to type ri -fo vs just ri -f isn't really that bad IMHO.
I agree with Kirk that the present capabilities of abbreviating parameters to the minimum that is unambiguous is sufficient. Add aliases to parameters is an unnecessary complication
Kirk Munro (@KirkMunro) the committee agreed that user defined parameter aliases would be useful for interactive use (aliases should never be used in scripts and we should have a corresponding rule for this) where rm is aliased to remove-item and -rf is aliased to -recurse -force. seemsl like this should be a RFC to get more feedback.
changed the title [-]Alias parameter -F to -Force for Remove-Item[/-][+]Support user defined parameter aliases, e.g. -F to -Force for Remove-Item[/+]on Nov 21, 2016
Limiting to interactive scenarios would be most important for this to work. I'm not a Bash user. If I inherit a script that someone wrote with Linux aliases, it would make it much harder to understand the script.
PowerShell needs to maintain consistency with scripts across platforms in order to make it easier for scripters to maintain in the long-term.
I don't see any reason to invest resources into providing this feature. Parameter aliases add an unnecessary layer of complexity. Going back to the original post, there's no reason to run a command like rm -f, when you can just as easily type rm -f<tab> and autocomplete the parameter. I'd make a case for tab-completing the cmdlet name as well. If there are parameter aliases that a user might be expecting to use, that should be well documented.
Upon further thinking on this, I don't know if this is worth a feature to enable custom parameter aliases if the primary (only?) use case is rm -rf. So a feature would not be trivial and currently without other real world scenarios, seems potentially little benefit vs the cost. For Remove-Item specifically, I have personally encountered the displeasure of having to use rm -r -fo (since -filter conflicts) and prefer rm -rf for expedient typing when used interactively.
So one possible alternate solution is to add a new -RecurseForce switch in a new parameterset which has the -rf alias. This should be a pretty small change. I think there is a question whether this sets a bad precedent.
So one possible alternate solution is to add a new -RecurseForce switch in a new parameterset which has the -rf alias. This should be a pretty small change. I think there is a question whether this sets a bad precedent.
It does, but I think if it solved the problem it would be worth it. I would bet that just about every single person who used PowerShell extensively has been annoyed by this at some point.
That said, I think the main frustration is "I feel like should be able to do this but that throws, now I gotta do that". I don't think the pain point is specifically the extra character, so I'm not sure how much this would actually help in the end.
It is worth discussing whether the parameter binder should, as a final fallback if nothing matches the given parameter name unambiguously, mimic behaviour from how these tools handle shorthand switches?
e.g., Remove-Item -rf where -rf is not able to be matched to any singular parameter name, should it enumerate switch parameters and see if it can unambiguously assign multiple parameters, one to each character specified?
@PowerShell/wg-powershell-cmdlets has discussed this, and despite the fact that a couple of us have the rm -rf muscle memory, we are rejecting this proposal for the following reasons:
it creates an unreasonable expectation of consistency between platforms (e.g. rm -rf on macOS/Linux actually executes /bin/rm while it would execute Remove-Item on Windows)
the implementation is complex and rife with potential for regression around the parameter binder
We also don't want to pursue the -RecurseForce as a one-off for Remove-Item because of the precedent that it sets with creating arguments like that for other cmdlets
If folks feel very passionately about having this functionality, the community is welcome to create a Gallery module that has well-formed wrapper functions for common scenarios like this. That module could even end up being configurable for folks with slightly different muscle memory requirements
Activity
[-]Bind `Remove-Item`'s `-f` to `-Force` only.[/-][+]Alias parameter -F to -Force for Remove-Item[/+]lzybkr commentedon Sep 7, 2016
Having
Remove-Item -Fwork just likeRemove-Item -Forceseems like a good idea.There is a small concern -
Remove-Itemis a cmdlet that is implemented by providers (like the file system, registery, environment, etc) and providers can add dynamic parameters. Providers that add dynamic parameters aren't all that common, but there is a risk that a provider adds a dynamic parameter with an identical alias-Fwhich would be masked by this change.In an ideal world, this wouldn't break scripts because they should be using full parameter names, but we don't live in that ideal world.
It feels like an unlikely breaking change, but we should have data to confirm that assumption before considering this enhancement.
be5invis commentedon Sep 7, 2016
Jason Shirk (@lzybkr) The current Remove-Item’s -f is... ambiguous (-Force and -Filter), so scripts cannot use it directly.
lzybkr commentedon Sep 7, 2016
That's not necessarily true if, as I previously stated, a provider adds a dynamic parameter with an alias.
be5invis commentedon Sep 7, 2016
Jason Shirk (@lzybkr) At least can alias
-ffor file provider, or other existing providers with only-Force.lzybkr commentedon Sep 7, 2016
Belleve (@be5invis) - in theory, sure, but I think that would be difficult to implement correctly.
Dynamic parameters don't support adding an alias to an existing parameter and we couldn't move
-Forceto be dynamic for a specific set of providers because the parameter must exist for all providers.GeeLaw commentedon Sep 7, 2016
I'm proposing a way to implement this... Allow specifying the precedence of a parameter when ambiguity happens. E.g.
[Parameter(Precedence = 1)][switch]$Force, [Parameter(Precedence = 0)][string]$Filter, then-F, if not otherwise resolved, would be-Force.For backward compatibility,
Precedenceshould beSystem.Nullable<System.Int32>orSystem.Nullable<System.UInt32>. Resolution step regarding precedence (of course full name and aliases should be used to resolve parameters before we come to this step) should be:Precedence.HasValue, the resolution fails;Precedence.HasValuethat has the largestPrecedence.Value, say this is collectionA;Ais 1, the parameter resolves to the only element inA;This won't break current providers that adds a dynamic parameter / an alias of a dynamic parameter
-F, and is consistent in all providers -- You don't lose-Fas an alias for-Forcemagically because you are not using built-in providers, you lose it because-Fresolves before it reaches-Forceand-Filter.lzybkr commentedon Sep 7, 2016
Ji Luo (@GeeLaw) - that might work, but the parameter binder is already too complicated, so it's hard to say if it'd be worth adding this complexity.
iSazonov commentedon Oct 6, 2016
Using aliases degrades readability of scripts.
Using Foreach-Object better using foreach.
Using Where-Object better using where.
Using -Force better using -F.
GeeLaw commentedon Oct 6, 2016
In reply to Ilya (@iSazonov) : If you read carefully enough, you would find that the proposed alias
-Ffor-Forceis intended to be used interactively, shortening the type for a often-used switch.KirkMunro commentedon Oct 6, 2016
I'd vote against building in a shorthand aliases for parameters (such as -F for -Force) because it could lead to accidents.. It's pretty well known that you can abbreviate any parameter using the minimum number of characters required to unambiguously identify that parameter. If -F is explicitly aliased to -Force, and someone forgets about the conflict between -Force and -Filter because there aren't multiple parameters that start with -F on every command that has a -Force parameter, suddenly they may invoke a command with -Force when they really meant to invoke it with -Filter.
A better solution for this problem would be to unlock parameter aliases in PowerShell (they currently cannot be modified by end users), allowing PowerShell users to add parameter aliases that they want to use in their interactive work to their profile, much like they can add command aliases today. Then if someone specifically wants -F to be an alias for -Force in Remove-Item for ad-hoc work, they can make that decision and change it for themselves without forcing a potentially unwanted change on the rest of the community.
In this particular scenario, having to type ri -fo vs just ri -f isn't really that bad IMHO.
RichardSiddaway commentedon Oct 6, 2016
I agree with Kirk that the present capabilities of abbreviating parameters to the minimum that is unambiguous is sufficient. Add aliases to parameters is an unnecessary complication
iSazonov commentedon Oct 6, 2016
An user can add parameter alias by means of proxy function.
9 remaining items
KirkMunro commentedon Nov 21, 2016
Hemant Mahawar (@HemantMahawar) To be clear, which part of this did the @PowerShell/powershell-committee agree is useful?
SteveL-MSFT commentedon Nov 21, 2016
Kirk Munro (@KirkMunro) the committee agreed that user defined parameter aliases would be useful for interactive use (aliases should never be used in scripts and we should have a corresponding rule for this) where
rmis aliased toremove-itemand-rfis aliased to-recurse -force. seemsl like this should be a RFC to get more feedback.[-]Alias parameter -F to -Force for Remove-Item[/-][+]Support user defined parameter aliases, e.g. -F to -Force for Remove-Item[/+]dragonwolf83 commentedon Nov 25, 2016
Limiting to interactive scenarios would be most important for this to work. I'm not a Bash user. If I inherit a script that someone wrote with Linux aliases, it would make it much harder to understand the script.
PowerShell needs to maintain consistency with scripts across platforms in order to make it easier for scripters to maintain in the long-term.
jdhitsolutions commentedon Jun 14, 2021
I don't see any reason to invest resources into providing this feature. Parameter aliases add an unnecessary layer of complexity. Going back to the original post, there's no reason to run a command like
rm -f, when you can just as easily typerm -f<tab>and autocomplete the parameter. I'd make a case for tab-completing the cmdlet name as well. If there are parameter aliases that a user might be expecting to use, that should be well documented.SteveL-MSFT commentedon Jun 24, 2021
Upon further thinking on this, I don't know if this is worth a feature to enable custom parameter aliases if the primary (only?) use case is
rm -rf. So a feature would not be trivial and currently without other real world scenarios, seems potentially little benefit vs the cost. ForRemove-Itemspecifically, I have personally encountered the displeasure of having to userm -r -fo(since-filterconflicts) and preferrm -rffor expedient typing when used interactively.So one possible alternate solution is to add a new
-RecurseForceswitch in a new parameterset which has the-rfalias. This should be a pretty small change. I think there is a question whether this sets a bad precedent.iSazonov commentedon Jun 25, 2021
Yes. Users can create any wrappers based on functions in their profiles.
SeeminglyScience commentedon Jun 25, 2021
It does, but I think if it solved the problem it would be worth it. I would bet that just about every single person who used PowerShell extensively has been annoyed by this at some point.
That said, I think the main frustration is "I feel like should be able to do this but that throws, now I gotta do that". I don't think the pain point is specifically the extra character, so I'm not sure how much this would actually help in the end.
vexx32 commentedon Jun 25, 2021
It is worth discussing whether the parameter binder should, as a final fallback if nothing matches the given parameter name unambiguously, mimic behaviour from how these tools handle shorthand switches?
e.g.,
Remove-Item -rfwhere-rfis not able to be matched to any singular parameter name, should it enumerate switch parameters and see if it can unambiguously assign multiple parameters, one to each character specified?joeyaiello commentedon Jun 30, 2021
@PowerShell/wg-powershell-cmdlets has discussed this, and despite the fact that a couple of us have the
rm -rfmuscle memory, we are rejecting this proposal for the following reasons:rm -rfon macOS/Linux actually executes/bin/rmwhile it would executeRemove-Itemon Windows)We also don't want to pursue the
-RecurseForceas a one-off forRemove-Itembecause of the precedent that it sets with creating arguments like that for other cmdletsIf folks feel very passionately about having this functionality, the community is welcome to create a Gallery module that has well-formed wrapper functions for common scenarios like this. That module could even end up being configurable for folks with slightly different muscle memory requirements