This question has been asked before, but PowerShell has since gone through several phases that it even has a different executable name now (pwsh.exe
). All answers assume powershell.exe
will never change.
Is there cross-platform a way on .NET to get path to the latest (or preferably, user-preferred) PowerShell executable without resorting to "try to find pwsh.exe in the PATH first, if it doesn't work, try finding powershell.exe" trickery? Obviously, that approach can fail someday too if pwsh.exe changes its name again. Not to mention that a user's preferences might be different than what's installed on the system.
Even MS-DOS had this feature with COMSPEC
environment variable, let alone Unix and its SHELL
variable. I'm surprised that PowerShell doesn't have this.
Perhaps, it's possible to get that information from PowerShell's runtime variables, but I'll be calling it from an external .NET process.