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
Introduce mode that disables aliases in scripts #2000
Inspired by PR #1901, I suggest Microsoft create a new version of strict mode for better writing style. In this new version, suppress all aliases by default. (People really should not use aliases in scripts/modules!) Enforcing good command invoking style makes backward compatibility easier both for Microsoft and script writer.
Example:
Set-StrictMode-Version 123; # 123 is an imaginary version number.
iwr -Uri example.com; # This line fails since aliases are suppressed here.Set-Alias iwr Invoke-WebRequest;
iwr -Uri example.com; # This line succeeds since `iwr` is set explicitly in the scope.
If suppressing all aliases is not approachable, it at least should suppress all aliases that are identical to utilities that is popular in Linux/Unix.
And Microsoft should leave Linux/Unix aliases available to scripts that are not setting this version of strict mode and running on Windows PowerShell. Removing those aliases from Windows PowerShell interactive sessions is good. That'll be acceptable for compatibility and better consistency with PowerShell on other systems.
Reacted by Raynor Vliegendhart, Eric S. Weilnau, Zhiqiang Yu, Mikey Lombardi (He/Him), Jonas, Brian Scholer and Michael Klement
Unfortunately, I don't have a ton of historical context on Set-StrictMode like Bruce Payette (@BrucePay) or Lee Holmes (@LeeHolmes), but I get the sense it's more about syntactical language purity than best pratices.
For that reason, I'd actually lean more towards this being implemented via Script Analyzer, our static analysis tool. We already have an AvoidAlias rule over there, though it was aggressively derided for being too noisy on people who didn't feel like aliases in scripts were a problem. Given everything with #1901, maybe it's time to bump it up to an Error level severity and tell the pro-alias folks to override AvoidAlias in their PSSA settings files.
Reacted by Chengjun Xie, Ryan Yates, Nicholas M. Getchell, Staffan Gustafsson, Joel Bennett and Keith Hill
@> though it was aggressively derided for being too noisy
Joey Aiello (@joeyaiello) Go ahead, you can say it. You "told us so". ;-) Although, at the time I wasn't thinking about PS running on Linux and the implications. Yikes!
Guidance should be as always was to avoid Aliases in scripts & I've always supported the Rule for being noisy as the more experienced PowerShell scripter can always suppress the rule if they want to.
I think turning off aliases needs to be tiered a bit rather than a simple on/off. Not all aliases are equally bad. I see the same discussion in PSScriptAnalyzer because its too noisy for core scenarios.
Aliases like iwr, curl, gci, ls, and cat are bad for two reasons:
Too Short and hard to read
Aliases to existing executables like curl, ls, and cat
The most useful aliases are: Select, Where, Group, Sort, %.
I found when doing a lot with the pipeline, the % alias (ForEach-Object) is invaluable to actually making it readable because you can very quickly finding yourself with lots of nested foreach loops. Cutting that down to just % substantially simplifies the code. I know some people stay away from pipelines but I see % as important as | itself and once you get used to it, reads faster than foreach itself or ForEach-Object.
So at a minimum I think we should keep all of the Aliases for -Object since they are core fundamentals of PowerShell and reduce noise a lot. They are likely to be the most used aliases. The full list is below:
With IntelliSense in PowerShell (PSReadline, VS Code, ISE) alias profitableness tends to zero.
Currently VS Code warns about alias and allow replace it by two clicks. It is great feature.
In script/module text aliases is bad practice in all times. The situation may become even worse when we realize our Keywords RFC.
The great features of the VSCode PowerShell extension during editing notwithstanding, a strict mode of sorts that prevents running scripts with aliases (ideally, at parse time) sounds useful.
I agree with Troy Campbell (@dragonwolf83) that certain aliases, such as % and ? are indispensable, but I think allowing aliases that shadow native utilities (e.g., sort) are problematic.
So, I think only the following aliases should still be permitted:
built-in aliases (they are the only ones guaranteed to available - at least for a given PS version)...
... that don't shadow native utilities on any platform (in other words: the route that PS Core has already chosen)
Another thing worth preventing in scripts is the use of elastic syntax (e.g., -Expand for -ExpandProperty), because it threatens the long-term stability of the code (future parameters with the same prefix could render the previously unique prefix ambiguous).
(Not all such cases can be prevented however, such as in the case of indirect command execution via strings).
In a similar vein, it would be great if the VSCode PowerShell extension didn't just expand aliases to their underlying commands, but expand parameter-name prefixes to the full parameter names too - and possibly also for the recently proposed extension of elastic syntax to operators - see #4730.
Activity
[-]Register new file extensions for better writing style[/-][+]Set-StrictMode for better writing style[/+]joeyaiello commentedon Aug 22, 2016
Unfortunately, I don't have a ton of historical context on
Set-StrictModelike Bruce Payette (@BrucePay) or Lee Holmes (@LeeHolmes), but I get the sense it's more about syntactical language purity than best pratices.For that reason, I'd actually lean more towards this being implemented via Script Analyzer, our static analysis tool. We already have an
AvoidAliasrule over there, though it was aggressively derided for being too noisy on people who didn't feel like aliases in scripts were a problem. Given everything with #1901, maybe it's time to bump it up to anErrorlevel severity and tell the pro-alias folks to overrideAvoidAliasin their PSSA settings files.rkeithhill commentedon Aug 22, 2016
@> though it was aggressively derided for being too noisy
Joey Aiello (@joeyaiello) Go ahead, you can say it. You "told us so". ;-) Although, at the time I wasn't thinking about PS running on Linux and the implications. Yikes!
madper commentedon Aug 22, 2016
As a result when people calling
curl blahblah:PS will execute the binary curl.exe in PATH (if any) in Strict mode. And run iwr in Normal mode...
lzybkr commentedon Aug 22, 2016
Set-StrictMode is dynamic, but we introduced
usingin a way that makes this possible.At any rate, this should be an RFC, I suggest writing one up.
[-]Set-StrictMode for better writing style[/-][+]Introduce mode that disables aliases in scripts[/+]kilasuit commentedon Aug 24, 2016
Guidance should be as always was to avoid Aliases in scripts & I've always supported the Rule for being noisy as the more experienced PowerShell scripter can always suppress the rule if they want to.
dlwyatt commentedon Aug 24, 2016
There are some basic aliases that I'd leave alone, even if this gets implemented.
Select,Where, that sort of thing.iSazonov commentedon May 9, 2017
It seems we want something like:
dragonwolf83 commentedon May 10, 2017
I think turning off aliases needs to be tiered a bit rather than a simple on/off. Not all aliases are equally bad. I see the same discussion in PSScriptAnalyzer because its too noisy for core scenarios.
Aliases like
iwr,curl,gci,ls, andcatare bad for two reasons:The most useful aliases are:
Select,Where,Group,Sort,%.I found when doing a lot with the pipeline, the % alias (ForEach-Object) is invaluable to actually making it readable because you can very quickly finding yourself with lots of nested foreach loops. Cutting that down to just % substantially simplifies the code. I know some people stay away from pipelines but I see % as important as | itself and once you get used to it, reads faster than foreach itself or ForEach-Object.
So at a minimum I think we should keep all of the Aliases for -Object since they are core fundamentals of PowerShell and reduce noise a lot. They are likely to be the most used aliases. The full list is below:
iSazonov commentedon May 10, 2017
With IntelliSense in PowerShell (PSReadline, VS Code, ISE) alias profitableness tends to zero.
Currently VS Code warns about alias and allow replace it by two clicks. It is great feature.
In script/module text aliases is bad practice in all times. The situation may become even worse when we realize our Keywords RFC.
mklement0 commentedon Sep 1, 2017
The great features of the VSCode PowerShell extension during editing notwithstanding, a strict mode of sorts that prevents running scripts with aliases (ideally, at parse time) sounds useful.
I agree with Troy Campbell (@dragonwolf83) that certain aliases, such as
%and?are indispensable, but I think allowing aliases that shadow native utilities (e.g.,sort) are problematic.So, I think only the following aliases should still be permitted:
Another thing worth preventing in scripts is the use of elastic syntax (e.g.,
-Expandfor-ExpandProperty), because it threatens the long-term stability of the code (future parameters with the same prefix could render the previously unique prefix ambiguous).(Not all such cases can be prevented however, such as in the case of indirect command execution via strings).
In a similar vein, it would be great if the VSCode PowerShell extension didn't just expand aliases to their underlying commands, but expand parameter-name prefixes to the full parameter names too - and possibly also for the recently proposed extension of elastic syntax to operators - see #4730.
rkeithhill commentedon Jun 30, 2021
PSScriptAnalyzer handles this. See https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation/AvoidUsingCmdletAliases.md
Note that PSSA also provides support for a customizable allow list.
iSazonov commentedon Jul 8, 2021
Although bash seems to me ideologically poor and obsolete, even it does it much better as far as aliases are concerned.