question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Introduce mode that disables aliases in scripts

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
joeyaiellocommented, Aug 22, 2016

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 an Error level severity and tell the pro-alias folks to override AvoidAlias in their PSSA settings files.

1reaction
rkeithhillcommented, 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.

<div> GitHub</div><div>PowerShell/PSScriptAnalyzer</div><div>Download ScriptAnalyzer from PowerShellGallery. Contribute to PowerShell/PSScriptAnalyzer development by creating an account on GitHub.</div>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Disable functions/aliases in a sourced script - bash
Is it possible in a sourced script to disable all functions/aliases from the parent process (one which runs my script)? I.e. if a...
Read more >
Avoid using cmdlet aliases. · Issue #214
I would assume if a script is being run through the Analyzer it is being prepared for distribution. Aliases should then only be...
Read more >
making it so alias's work in shell scripts?
When the shell is started as a script interpreter, it does not load any external "rc" scripts and the alias is not defined....
Read more >
In Bash, when to alias, when to script and when to write a ...
For this case, use an alias / function set in the shell's "interactive-mode-only" rc file (for bash this is .bashrc ).
Read more >
Best Practice for Using Aliases in PowerShell Scripts
The first is that no alias is guaranteed to exist—even aliases that are created by Windows PowerShell. There are two classes (or types)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found