Skip to content

Commit 93b9e20

Browse files
authored
Update platform.ts
Checks for PowerShell Core on Windows OS, if exists uses the latest version of PowerShell as the default PowerShell path
1 parent ddbf324 commit 93b9e20

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/platform.ts

+32-10
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,39 @@ export function getDefaultPowerShellPath(
6969

7070
// Find the path to powershell.exe based on the current platform
7171
// and the user's desire to run the x86 version of PowerShell
72-
if (platformDetails.operatingSystem === OperatingSystem.Windows) {
73-
if (use32Bit) {
74-
powerShellExePath =
75-
platformDetails.isOS64Bit && platformDetails.isProcess64Bit
76-
? SysWow64PowerShellPath
77-
: System32PowerShellPath;
72+
if (platformDetails.operatingSystem === OperatingSystem.Windows) {
73+
const psCoreInstallPath =
74+
(!platformDetails.isProcess64Bit ? process.env.ProgramW6432 : process.env.ProgramFiles) + "\\PowerShell";
75+
if (fs.existsSync(psCoreInstallPath)) {
76+
const arch = platformDetails.isProcess64Bit ? "(x64)" : "(x86)";
77+
const psCorePaths =
78+
fs.readdirSync(psCoreInstallPath)
79+
.map((item) => path.join(psCoreInstallPath, item))
80+
.filter((item) => {
81+
const exePath = path.join(item, "pwsh.exe");
82+
return fs.lstatSync(item).isDirectory() && fs.existsSync(exePath);
83+
})
84+
.map((item) => ({
85+
versionName: `PowerShell Core ${path.parse(item).base} ${arch}`,
86+
exePath: path.join(item, "pwsh.exe"),
87+
}));
88+
89+
if (psCorePaths) {
90+
powerShellExePath = psCorePaths[0].exePath;
91+
}
92+
7893
} else {
79-
powerShellExePath =
80-
!platformDetails.isOS64Bit || platformDetails.isProcess64Bit
81-
? System32PowerShellPath
82-
: SysnativePowerShellPath;
94+
if (use32Bit) {
95+
powerShellExePath =
96+
platformDetails.isOS64Bit && platformDetails.isProcess64Bit
97+
? SysWow64PowerShellPath
98+
: System32PowerShellPath;
99+
} else {
100+
powerShellExePath =
101+
!platformDetails.isOS64Bit || platformDetails.isProcess64Bit
102+
? System32PowerShellPath
103+
: SysnativePowerShellPath;
104+
}
83105
}
84106
} else if (platformDetails.operatingSystem === OperatingSystem.MacOS) {
85107
// Always default to the stable version of PowerShell (if installed) but handle case of only Preview installed

0 commit comments

Comments
 (0)