@@ -66,27 +66,55 @@ export function getDefaultPowerShellPath(
66
66
use32Bit : boolean = false ) : string | null {
67
67
68
68
let powerShellExePath ;
69
+ let psCoreInstallPath ;
69
70
70
- // Find the path to powershell.exe based on the current platform
71
+ // Find the path to the powershell executable based on the current platform
71
72
// and the user's desire to run the x86 version of PowerShell
72
73
if ( platformDetails . operatingSystem === OperatingSystem . Windows ) {
73
74
if ( use32Bit ) {
74
- powerShellExePath =
75
- platformDetails . isOS64Bit && platformDetails . isProcess64Bit
76
- ? SysWow64PowerShellPath
77
- : System32PowerShellPath ;
75
+ psCoreInstallPath =
76
+ ( platformDetails . isProcess64Bit ? process . env [ "ProgramFiles(x86)" ] : process . env . ProgramFiles )
77
+ + "\\PowerShell" ;
78
78
} else {
79
- powerShellExePath =
80
- ! platformDetails . isOS64Bit || platformDetails . isProcess64Bit
81
- ? System32PowerShellPath
82
- : SysnativePowerShellPath ;
79
+ psCoreInstallPath =
80
+ ( platformDetails . isProcess64Bit ? process . env . ProgramFiles : process . env . ProgramW6432 ) + "\\PowerShell" ;
83
81
}
84
- } else if ( platformDetails . operatingSystem === OperatingSystem . MacOS ) {
82
+
83
+ if ( fs . existsSync ( psCoreInstallPath ) ) {
84
+ const arch = platformDetails . isProcess64Bit ? "(x64)" : "(x86)" ;
85
+ const psCorePaths =
86
+ fs . readdirSync ( psCoreInstallPath )
87
+ . map ( ( item ) => path . join ( psCoreInstallPath , item ) )
88
+ . filter ( ( item ) => {
89
+ const exePath = path . join ( item , "pwsh.exe" ) ;
90
+ return fs . lstatSync ( item ) . isDirectory ( ) && fs . existsSync ( exePath ) ;
91
+ } )
92
+ . map ( ( item ) => ( {
93
+ versionName : `PowerShell ${ path . parse ( item ) . base } ${ arch } ` ,
94
+ exePath : path . join ( item , "pwsh.exe" ) ,
95
+ } ) ) ;
96
+
97
+ if ( psCorePaths ) {
98
+ return powerShellExePath = psCorePaths [ 0 ] . exePath ;
99
+ }
100
+ }
101
+
102
+ // No PowerShell 6+ detected so use Windows PowerShell.
103
+ if ( use32Bit ) {
104
+ return platformDetails . isOS64Bit && platformDetails . isProcess64Bit
105
+ ? SysWow64PowerShellPath
106
+ : System32PowerShellPath ;
107
+ }
108
+ return ! platformDetails . isOS64Bit || platformDetails . isProcess64Bit
109
+ ? System32PowerShellPath
110
+ : SysnativePowerShellPath ;
111
+ }
112
+ if ( platformDetails . operatingSystem === OperatingSystem . MacOS ) {
85
113
// Always default to the stable version of PowerShell (if installed) but handle case of only Preview installed
86
114
powerShellExePath = macOSExePath ;
87
115
if ( ! fs . existsSync ( macOSExePath ) && fs . existsSync ( macOSPreviewExePath ) ) {
88
116
powerShellExePath = macOSPreviewExePath ;
89
- }
117
+ }
90
118
} else if ( platformDetails . operatingSystem === OperatingSystem . Linux ) {
91
119
// Always default to the stable version of PowerShell (if installed) but handle case of only Preview installed
92
120
// as well as the Snaps case - https://snapcraft.io/
@@ -179,7 +207,7 @@ export function getAvailablePowerShellExes(
179
207
return fs . lstatSync ( item ) . isDirectory ( ) && fs . existsSync ( exePath ) ;
180
208
} )
181
209
. map ( ( item ) => ( {
182
- versionName : `PowerShell Core ${ path . parse ( item ) . base } ${ arch } ` ,
210
+ versionName : `PowerShell ${ path . parse ( item ) . base } ${ arch } ` ,
183
211
exePath : path . join ( item , "pwsh.exe" ) ,
184
212
} ) ) ;
185
213
@@ -200,7 +228,7 @@ export function getAvailablePowerShellExes(
200
228
exePaths . forEach ( ( exePath ) => {
201
229
if ( fs . existsSync ( exePath ) ) {
202
230
paths . push ( {
203
- versionName : "PowerShell Core " + ( / - p r e v i e w / . test ( exePath ) ? " Preview" : "" ) ,
231
+ versionName : "PowerShell" + ( / - p r e v i e w / . test ( exePath ) ? " Preview" : "" ) ,
204
232
exePath,
205
233
} ) ;
206
234
}
0 commit comments