elvish icon indicating copy to clipboard operation
elvish copied to clipboard

Prefer powershell script over cmd scripts

Open balthild opened this issue 1 year ago • 4 comments

Closes #1853

balthild avatar Nov 06 '24 13:11 balthild

Thanks, I like the idea in principle, but looking at the implementation, a problem is that there are actually two different PowerShell's:

  • The classical "Windows PowerShell" that comes pre-installed in modern Windows versions, with the binary name powershell.exe.
  • The modern "PowerShell Core" that needs to be installed by the user, with the binary name pwsh.exe.

As far as I know, the modern pwsh.exe is not completely backwards compatible with the classical powershell.exe. The current implementation picks powershell.exe, but I don't feel Elvish should be making the choice on the user's behalf.

We could in principle implement an option for choosing which PowerShell to use, but I feel this would be overcomplicating what's supposed to simplify things for users.

I noticed that PowerShell itself will happily execute a .ps1 file in PATH (both powershell.exe and pwsh.exe, although the former has an annoying policy thing that prevents it by default), but then PowerShell knows what its own path is, and if you're already using say pwsh.exe, this is arguably some signal that you prefer it. The corollary seems to be that Elvish is in a position to special-case .elv files on Windows...

All in all I'm leaning towards rejecting this PR. Let me know if I missed something.

xiaq avatar Jan 03 '25 22:01 xiaq

Maybe a solution is to find out which program that .ps1 files are associated with, but it requires some extra work to implement. If you think this works, I may take some time to try figuring it out.

balthild avatar Jan 15 '25 04:01 balthild

Hello! Recently, I found in an MS doc that pwsh.exe could be compatible with powershell.exe through .NET Standard 2.0:

With the advent of .NET Standard 2.0, PowerShell can load many traditional Windows PowerShell modules without modification. Additionally, PowerShell 7 includes a Windows PowerShell Compatibility feature that allows you to use Windows PowerShell modules that still require the full framework.

Maybe it's OK to just try pwsh.exe first, and fallback to powershell.exe if pwsh.exe doesn't exist?

Also, since PowerShell Core is cross-platform, their script usually contains a shebang (#!/usr/bin/env pwsh). Maybe a strategy is to only use pwsh.exe if a shebang exists?

balthild avatar Jan 26 '25 20:01 balthild

Maybe it's OK to just try pwsh.exe first, and fallback to powershell.exe if pwsh.exe doesn't exist?

Update: Just found that this is the behavior of cmd.

balthild avatar Feb 19 '25 04:02 balthild