Check powershell version before building #19330

Closed
opened 2026-01-31 06:40:18 +00:00 by claunia · 8 comments
Owner

Originally created by @Smeyer025 on GitHub (Feb 6, 2023).

Description of the new feature/enhancement

When trying to build for the first time, if the wrong version of PowerShell is installed, the build fails without a clear error message detailing the issue. For the sake of developers new to the project, an error message pointing out the incorrect version of PowerShell could be very helpful.

This is my first time contributing, so any feedback is welcome! I would also like to try and implement this if that's alright.

Originally created by @Smeyer025 on GitHub (Feb 6, 2023). # Description of the new feature/enhancement When trying to build for the first time, if the wrong version of PowerShell is installed, the build fails without a clear error message detailing the issue. For the sake of developers new to the project, an error message pointing out the incorrect version of PowerShell could be very helpful. This is my first time contributing, so any feedback is welcome! I would also like to try and implement this if that's alright.
Author
Owner

@Smeyer025 commented on GitHub (Feb 7, 2023):

To add some more info: when building for the first time, I had Windows Powershell installed, which I thought was the version I needed. The build failed, with 6 different Projects failing to build.

console
(Note: In this image, I ran it as AnyCPU instead of x64. When I ran it as x64 before I downloaded the correct version of PowerShell, I got the same errors.)

I thought I had the correct version of PowerShell installed, but what tipped me off was this line:
image

I downloaded PowerShell 7.3.2, restarted Visual Studio, and it built without issue. I think it would be a good idea for new users to have a more clear error stating that the version of PowerShell is incorrect, so it would be helpful if there was a pre-build check to see what version of PS is installed and an error message if the version installed is incorrect.

I'm new to the project and to open source, so any advice or feedback would be welcome. As I said, I would love to tackle this issue if possible.

@Smeyer025 commented on GitHub (Feb 7, 2023): To add some more info: when building for the first time, I had Windows Powershell installed, which I thought was the version I needed. The build failed, with 6 different Projects failing to build. ![console](https://user-images.githubusercontent.com/81830231/217281724-d8937453-72b2-4445-b349-4a7eb4f0ce51.PNG) (Note: In this image, I ran it as AnyCPU instead of x64. When I ran it as x64 before I downloaded the correct version of PowerShell, I got the same errors.) I thought I had the correct version of PowerShell installed, but what tipped me off was this line: ![image](https://user-images.githubusercontent.com/81830231/217282837-183fd0df-9f3e-4bf4-9796-179ae2a8c6fb.png) I downloaded PowerShell 7.3.2, restarted Visual Studio, and it built without issue. I think it would be a good idea for new users to have a more clear error stating that the version of PowerShell is incorrect, so it would be helpful if there was a pre-build check to see what version of PS is installed and an error message if the version installed is incorrect. I'm new to the project and to open source, so any advice or feedback would be welcome. As I said, I would love to tackle this issue if possible.
Author
Owner

@carlos-zamora commented on GitHub (Feb 8, 2023):

I'm new to the project and to open source, so any advice or feedback would be welcome. As I said, I would love to tackle this issue if possible.

We would love to accept a contribution for this!

A good first step would be to find the vcxproj file that uses pwsh to see if any improvements can be made in there.

@carlos-zamora commented on GitHub (Feb 8, 2023): > I'm new to the project and to open source, so any advice or feedback would be welcome. As I said, I would love to tackle this issue if possible. We would love to accept a contribution for this! A good first step would be to find the vcxproj file that uses pwsh to see if any improvements can be made in there.
Author
Owner

@heysujal commented on GitHub (Mar 2, 2023):

@Smeyer025 are you still working on this ?
@carlos-zamora @zadjii-msft I was able to find file which is Microsoft.Terminal.Settings.ModelLib.vcxproj . I have not run this project in my local system yet. Now the line 334 seems to be in question (as in the screenshot) which is a comment(How can a comment give error ?).
Now where do I have to go from here . Suggest some things that I need to learn to proceed. I already read the CONTRIBUTING.md .

@heysujal commented on GitHub (Mar 2, 2023): @Smeyer025 are you still working on this ? @carlos-zamora @zadjii-msft I was able to find file which is `Microsoft.Terminal.Settings.ModelLib.vcxproj` . I have not run this project in my local system yet. Now the line 334 seems to be in question (as in the screenshot) which is a comment(How can a comment give error ?). Now where do I have to go from here . Suggest some things that I need to learn to proceed. I already read the CONTRIBUTING.md .
Author
Owner

@Smeyer025 commented on GitHub (Mar 2, 2023):

@heysujal I am still working on this, and I think I have a solution. Here's what I have so far:
image
This error occurs because PowerShell Core (v7 or above, as stated in the prerequisites) is not installed. That being said, my issue when building initially was that I had Windows PowerShell 5.1 installed, and though the command to find the version is the same in both PowerShell Core and Windows PowerShell ($PSVersionTable, or $PSVersionTable.PSVersion.Major to be more specific), the name of the .exe file is different between Windows and Core. For Windows PowerShell, it is powershell.exe as opposed to pwsh.exe for Core. Here's what I propose as a solution, that I have implemented in the .vcxproj file:
image
What this does is it calls scripts I wrote that check the version of PowerShell installed. For pwsh.exe (PowerShell Core), if the version installed is less than 7.0.0, it writes an error and exits. But, for powershell.exe (Windows Powershell), it only checks to see if it is installed. If it is installed, it writes an error directing the user to install PowerShell Core, but will not exit, in case the user has both Core and Windows installed. Here are the exact scripts:
CheckPSVersion.ps1:

if($PSVersionTable.PSVersion.Major -lt 7){
   Write-Error "Incorrect version of PowerShell installed.`nMake sure you have at least PowerShell Core 7.0.0."
   Exit 1 
}

WindowsCheckPSVersion.ps1

if($PSVersionTable.PSVersion.Major -le 5){
   Write-Error "Make sure you have at least PowerShell Core 7.0.0."
}

(Note: I chose not to have WindowsCheckPSVersion.ps1 exit because if a user has both Windows and Core installed, it would exit even though there would be no issues building)

When building with these scripts here are the different cases I validated:
Core 7.3.2 and Windows 5.1 installed: built without issue, an error was printed to ensure that Core 7.0.0 or above was installed.
Core installed, but not Windows: built without issue; threw an error that "powershell.exe" (Windows Powershell) was not recognized.
Windows installed, but not Core: Build failed, but the user is instructed to download Core by WindowsCheckPSVersion.ps1
Neither Core nor Windows installed: Build failed, both commands fail because pwsh.exe and powershell.exe are both not recognized. But, it is easier to decipher for newer users because there is an explicit error saying "powershell.exe" is not recognized, so at worst, someone might download Windows PowerShell, which would lead to one of the above cases on rebuild.

@carlos-zamora @zadjii-msft I would love some feedback on this solution, so any comments or concerns would be much appreciated. If this solution is good enough for a pull request, let me know and I will submit one immediately. Thanks!

@Smeyer025 commented on GitHub (Mar 2, 2023): @heysujal I am still working on this, and I think I have a solution. Here's what I have so far: ![image](https://user-images.githubusercontent.com/81830231/222504322-ce263214-b591-4fb1-888d-0fe97b1e3981.png) This error occurs because PowerShell Core (v7 or above, as stated in the prerequisites) is not installed. That being said, my issue when building initially was that I had Windows PowerShell 5.1 installed, and though the command to find the version is the same in both PowerShell Core and Windows PowerShell (`$PSVersionTable`, or `$PSVersionTable.PSVersion.Major` to be more specific), the name of the .exe file is different between Windows and Core. For Windows PowerShell, it is powershell.exe as opposed to pwsh.exe for Core. Here's what I propose as a solution, that I have implemented in the .vcxproj file: ![image](https://user-images.githubusercontent.com/81830231/222509612-3d40b77b-7759-4958-be6e-90822747200b.png) What this does is it calls scripts I wrote that check the version of PowerShell installed. For pwsh.exe (PowerShell Core), if the version installed is less than 7.0.0, it writes an error and exits. But, for powershell.exe (Windows Powershell), it only checks to see if it is installed. If it is installed, it writes an error directing the user to install PowerShell Core, but will not exit, in case the user has both Core and Windows installed. Here are the exact scripts: CheckPSVersion.ps1: ``` if($PSVersionTable.PSVersion.Major -lt 7){ Write-Error "Incorrect version of PowerShell installed.`nMake sure you have at least PowerShell Core 7.0.0." Exit 1 } ``` WindowsCheckPSVersion.ps1 ``` if($PSVersionTable.PSVersion.Major -le 5){ Write-Error "Make sure you have at least PowerShell Core 7.0.0." } ``` (Note: I chose not to have WindowsCheckPSVersion.ps1 exit because if a user has both Windows and Core installed, it would exit even though there would be no issues building) When building with these scripts here are the different cases I validated: **Core 7.3.2 and Windows 5.1 installed**: built without issue, an error was printed to ensure that Core 7.0.0 or above was installed. **Core installed, but not Windows**: built without issue; threw an error that "powershell.exe" (Windows Powershell) was not recognized. **Windows installed, but not Core**: Build failed, but the user is instructed to download Core by WindowsCheckPSVersion.ps1 **Neither Core nor Windows installed**: Build failed, both commands fail because pwsh.exe and powershell.exe are both not recognized. But, it is easier to decipher for newer users because there is an explicit error saying "powershell.exe" is not recognized, so at worst, someone might download Windows PowerShell, which would lead to one of the above cases on rebuild. @carlos-zamora @zadjii-msft I would love some feedback on this solution, so any comments or concerns would be much appreciated. If this solution is good enough for a pull request, let me know and I will submit one immediately. Thanks!
Author
Owner

@zadjii-msft commented on GitHub (Mar 2, 2023):

Hmm. That seems like a good start, though I'm a little worried that even in the case where pwsh 7 is installed, we still print an error message.

Maybe it'd just be possible to check in the WindowsCheckPSVersion.ps1 script if pwsh is installed on the path, and then print the error and bail if we fail to find it? My PowerShell-fu is notoriously the weakest of anyone on the team, but maybe something like:

if (Get-Command "pwsh.exe" -ErrorAction SilentlyContinue) {
  "yep"
} else { 
  Write-host "nope"
}

image

@zadjii-msft commented on GitHub (Mar 2, 2023): Hmm. That seems like a good start, though I'm a little worried that even in the case where pwsh 7 is installed, we still print an error message. Maybe it'd just be possible to check in the `WindowsCheckPSVersion.ps1` script if `pwsh` is installed on the path, and then print the error and bail if we fail to find it? My PowerShell-fu is notoriously the weakest of anyone on the team, but maybe something like: ```ps1 if (Get-Command "pwsh.exe" -ErrorAction SilentlyContinue) { "yep" } else { Write-host "nope" } ``` ![image](https://user-images.githubusercontent.com/18356694/222576801-2a7212e5-2c41-49c7-afb6-70a0342a62c6.png)
Author
Owner

@Smeyer025 commented on GitHub (Mar 3, 2023):

@zadjii-msft Thanks for the feedback! I changed WindowsCheckPSVersion.ps1 to be:

if(-not (Get-Command "pwsh.exe" -ErrorAction SilentlyContinue)){
    Write-Error "Incorrect version of PowerShell installed.`nMake sure you have at least PowerShell Core 7.0.0."
}

This now only outputs an error if Windows PowerShell is installed and PowerShell Core is not.

Now, the updated build for the cases from before:
Core 7.3.2 and Windows 5.1 installed: built without issue, no erroneous errors
Core installed, but not Windows: built without issue; threw an error that "powershell.exe" (Windows Powershell) was not recognized.
Windows installed, but not Core: Build failed, and the user is instructed to download Core by WindowsCheckPSVersion.ps1
Neither Core nor Windows installed: Build failed, both commands fail because pwsh.exe and powershell.exe are both not recognized. But, it is easier to decipher for newer users because there is an explicit error saying "powershell.exe" is not recognized, so at worst, someone might download Windows PowerShell, which would lead to one of the above cases on rebuild.

Thanks so much for the quick response! I would love feedback on my updated script, and thanks again!

@Smeyer025 commented on GitHub (Mar 3, 2023): @zadjii-msft Thanks for the feedback! I changed `WindowsCheckPSVersion.ps1` to be: ``` if(-not (Get-Command "pwsh.exe" -ErrorAction SilentlyContinue)){ Write-Error "Incorrect version of PowerShell installed.`nMake sure you have at least PowerShell Core 7.0.0." } ``` This now only outputs an error if Windows PowerShell is installed and PowerShell Core is not. Now, the updated build for the cases from before: **Core 7.3.2 and Windows 5.1 installed:** built without issue, no erroneous errors **Core installed, but not Windows:** built without issue; threw an error that "powershell.exe" (Windows Powershell) was not recognized. **Windows installed, but not Core:** Build failed, and the user is instructed to download Core by WindowsCheckPSVersion.ps1 **Neither Core nor Windows installed:** Build failed, both commands fail because pwsh.exe and powershell.exe are both not recognized. But, it is easier to decipher for newer users because there is an explicit error saying "powershell.exe" is not recognized, so at worst, someone might download Windows PowerShell, which would lead to one of the above cases on rebuild. Thanks so much for the quick response! I would love feedback on my updated script, and thanks again!
Author
Owner

@zadjii-msft commented on GitHub (Mar 3, 2023):

Seems good to me! Looking forward to the PR ☺️

@zadjii-msft commented on GitHub (Mar 3, 2023): Seems good to me! Looking forward to the PR ☺️
Author
Owner

@Smeyer025 commented on GitHub (Mar 3, 2023):

Seems good to me! Looking forward to the PR ☺️

Awesome, I'll start working on that right now!

@Smeyer025 commented on GitHub (Mar 3, 2023): > Seems good to me! Looking forward to the PR ☺️ Awesome, I'll start working on that right now!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#19330