Is there any easy way to invoke a powershell script in separate process while being cross-platform (i.e. supporting both powershell.exe and pwsh.exe, and ideally powershell on linux (not required)).
If only supporting powershell.exe would be requirement I know I can do
$powershellPath = "$env:windir\system32\windowspowershell\v1.0\powershell.exe"
$process = Start-Process $powershellPath -NoNewWindow -ArgumentList ("-ExecutionPolicy Bypass -noninteractive -noprofile " + $scriptBlock) -PassThru
$process.WaitForExit($waitTimeMilliseconds)
I could see that I could try to find out if I'm running powershell.exe or pwsh.exe and do some heuristic for the powershell path but it all seems weirdly convoluted... isn't here some better way?
A slightly better approach might be to use $PSHome variable, e.g.: via
& $PSHOME\powershell.exe -Command '$PID'
But even that has the issue of pwsh pshome not containing powershell.exe but rather only pwsh.exe.