Introduce mode that disables aliases in scripts
See original GitHub issueInspired 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.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:7
- Comments:12 (4 by maintainers)
Unfortunately, I don’t have a ton of historical context on
Set-StrictMode
like @BrucePay or @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 anError
level severity and tell the pro-alias folks to overrideAvoidAlias
in their PSSA settings files.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.